如何将 WSDL 实现到 ASMX

How to implement WSDL to ASMX

我有这个 WSDL,应该用于 IIS 服务器内的 inbound/outbound 消息。我已经使用 "wsdl thefile.wsdl /l:VB" 生成了一个 .vb 文件,但现在我不确定该怎么做。我只应该编写一个方法来保存入站消息。

<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://www.softwriters.com/fwhl7/1.0/"
xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://www.softwriters.com/fwhl7/1.0/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
 <wsdl:types>
 <s:schema elementFormDefault="qualified" targetNamespace="http://www.softwriters.com/fwhl7/1.0/">
 <s:element name="SendMessage">
 <s:complexType>
 <s:sequence>
 <s:element minOccurs="0" maxOccurs="1" name="userId" type="s:string" />
 <s:element minOccurs="0" maxOccurs="1" name="passwordDigest" type="s:base64Binary" />
 <s:element minOccurs="0" maxOccurs="1" name="nonce" type="s:string" />
 <s:element minOccurs="1" maxOccurs="1" name="ts" type="s:dateTime" />
 <s:element minOccurs="0" maxOccurs="1" name="message" type="s:base64Binary" />
 </s:sequence>
 </s:complexType>
 </s:element>
 <s:element name="SendMessageResponse">
 <s:complexType>
 <s:sequence>
 <s:element minOccurs="0" maxOccurs="1" name="SendMessageResult" type="s:base64Binary" />
 </s:sequence>
 </s:complexType>
 </s:element>
 </s:schema>
 </wsdl:types>
 <wsdl:message name="SendMessageSoapIn">
 <wsdl:part name="parameters" element="tns:SendMessage" />
 </wsdl:message>
 <wsdl:message name="SendMessageSoapOut">
 <wsdl:part name="parameters" element="tns:SendMessageResponse" />
 </wsdl:message>
 <wsdl:portType name="FwHL7Soap">
 <wsdl:operation name="SendMessage">
 <wsdl:input message="tns:SendMessageSoapIn" />
 <wsdl:output message="tns:SendMessageSoapOut" />
 </wsdl:operation>
 </wsdl:portType>
 <wsdl:binding name="FwHL7Soap" type="tns:FwHL7Soap">
 <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
 <wsdl:operation name="SendMessage">
 <soap:operation soapAction="http://www.softwriters.com/fwhl7/1.0/SendMessage" style="document" />
 <wsdl:input>
 <soap:body use="literal" />
 </wsdl:input>
  <wsdl:output>
 <soap:body use="literal" />
 </wsdl:output>
 </wsdl:operation>
 </wsdl:binding>
 <wsdl:binding name="FwHL7Soap12" type="tns:FwHL7Soap">
 <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
 <wsdl:operation name="SendMessage">
 <soap12:operation soapAction="http://www.softwriters.com/fwhl7/1.0/SendMessage" style="document" />
 <wsdl:input>
 <soap12:body use="literal" />
 </wsdl:input>
 <wsdl:output>
 <soap12:body use="literal" />
 </wsdl:output>
 </wsdl:operation>
 </wsdl:binding>
 <wsdl:service name="FwHL7">
 <wsdl:port name="FwHL7Soap" binding="tns:FwHL7Soap">
 <soap:address location="http://localhost:53012/FwHL7.asmx" />
 </wsdl:port>
 <wsdl:port name="FwHL7Soap12" binding="tns:FwHL7Soap12">
 <soap12:address location="http://localhost:53012/FwHL7.asmx" />
 </wsdl:port>
 </wsdl:service>
</wsdl:definitions>

我创建了一个新的 ASP.NET Web 服务项目,将 WSDL 添加到服务引用中,并导入了生成的 .vb。我在项目中创建了一个 ASMX 文件,但不确定从那里去哪里。查看界面,我假设我应该将 WebMethod() 添加到适用的方法中:

Option Strict Off
Option Explicit On
'------------------------------------------------------------------------------
' <auto-generated>
'     This code was generated by a tool.
'     Runtime Version:4.0.30319.42000
'
'     Changes to this file may cause incorrect behavior and will be lost if
'     the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------

Imports System.Web.Services

<System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0"),
 System.ServiceModel.ServiceContractAttribute([Namespace]:="http://www.softwriters.com/fwhl7/1.0/", ConfigurationName:="FwHL7Soap")>
Public Interface FwHL7Soap

    'CODEGEN: Generating message contract since element name userId from namespace http://www.softwriters.com/fwhl7/1.0/ is not marked nillable
    <System.ServiceModel.OperationContractAttribute(Action:="http://www.softwriters.com/fwhl7/1.0/SendMessage", ReplyAction:="*")>
    Function SendMessage(ByVal request As SendMessageRequest) As SendMessageResponse

    <System.ServiceModel.OperationContractAttribute(Action:="http://www.softwriters.com/fwhl7/1.0/SendMessage", ReplyAction:="*")>
    Function SendMessageAsync(ByVal request As SendMessageRequest) As System.Threading.Tasks.Task(Of SendMessageResponse)
End Interface

<System.Diagnostics.DebuggerStepThroughAttribute(),
 System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0"),
 System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced),
 System.ServiceModel.MessageContractAttribute(IsWrapped:=False)>
Partial Public Class SendMessageRequest

    <System.ServiceModel.MessageBodyMemberAttribute(Name:="SendMessage", [Namespace]:="http://www.softwriters.com/fwhl7/1.0/", Order:=0)>
    Public Body As SendMessageRequestBody

    Public Sub New()
        MyBase.New
    End Sub

    Public Sub New(ByVal Body As SendMessageRequestBody)
        MyBase.New
        Me.Body = Body
    End Sub
End Class

<System.Diagnostics.DebuggerStepThroughAttribute(),
 System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0"),
 System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced),
 System.Runtime.Serialization.DataContractAttribute([Namespace]:="http://www.softwriters.com/fwhl7/1.0/")>
Partial Public Class SendMessageRequestBody

    <System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue:=False, Order:=0)>
    Public userId As String

    <System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue:=False, Order:=1)>
    Public passwordDigest() As Byte

    <System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue:=False, Order:=2)>
    Public nonce As String

    <System.Runtime.Serialization.DataMemberAttribute(Order:=3)>
    Public ts As Date

    <System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue:=False, Order:=4)>
    Public message() As Byte

    Public Sub New()
        MyBase.New
    End Sub

    Public Sub New(ByVal userId As String, ByVal passwordDigest() As Byte, ByVal nonce As String, ByVal ts As Date, ByVal message() As Byte)
        MyBase.New
        Me.userId = userId
        Me.passwordDigest = passwordDigest
        Me.nonce = nonce
        Me.ts = ts
        Me.message = message
    End Sub
End Class

<System.Diagnostics.DebuggerStepThroughAttribute(),
 System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0"),
 System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced),
 System.ServiceModel.MessageContractAttribute(IsWrapped:=False)>
Partial Public Class SendMessageResponse

    <System.ServiceModel.MessageBodyMemberAttribute(Name:="SendMessageResponse", [Namespace]:="http://www.softwriters.com/fwhl7/1.0/", Order:=0)>
    Public Body As SendMessageResponseBody

    Public Sub New()
        MyBase.New
    End Sub

    Public Sub New(ByVal Body As SendMessageResponseBody)
        MyBase.New
        Me.Body = Body

    End Sub
End Class

<System.Diagnostics.DebuggerStepThroughAttribute(),
 System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0"),
 System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced),
 System.Runtime.Serialization.DataContractAttribute([Namespace]:="http://www.softwriters.com/fwhl7/1.0/")>
Partial Public Class SendMessageResponseBody

    <System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue:=False, Order:=0)>
    Public SendMessageResult() As Byte

    Public Sub New()
        MyBase.New
    End Sub

    Public Sub New(ByVal SendMessageResult() As Byte)
        MyBase.New
        Me.SendMessageResult = SendMessageResult
    End Sub
End Class

<System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")>
Public Interface FwHL7SoapChannel
    Inherits FwHL7Soap, System.ServiceModel.IClientChannel
End Interface

<System.Diagnostics.DebuggerStepThroughAttribute(),
 System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")>
Partial Public Class FwHL7SoapClient
    Inherits System.ServiceModel.ClientBase(Of FwHL7Soap)
    Implements FwHL7Soap

    Public Sub New()
        MyBase.New
    End Sub

    Public Sub New(ByVal endpointConfigurationName As String)
        MyBase.New(endpointConfigurationName)
    End Sub

    Public Sub New(ByVal endpointConfigurationName As String, ByVal remoteAddress As String)
        MyBase.New(endpointConfigurationName, remoteAddress)
    End Sub

    Public Sub New(ByVal endpointConfigurationName As String, ByVal remoteAddress As System.ServiceModel.EndpointAddress)
        MyBase.New(endpointConfigurationName, remoteAddress)
    End Sub

    Public Sub New(ByVal binding As System.ServiceModel.Channels.Binding, ByVal remoteAddress As System.ServiceModel.EndpointAddress)
        MyBase.New(binding, remoteAddress)
    End Sub

    <System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)>
    Function FwHL7Soap_SendMessage(ByVal request As SendMessageRequest) As SendMessageResponse Implements FwHL7Soap.SendMessage
        Return MyBase.Channel.SendMessage(request)
    End Function

    <WebMethod()>
    Public Function SendMessage(ByVal userId As String, ByVal passwordDigest() As Byte, ByVal nonce As String, ByVal ts As Date, ByVal message() As Byte) As Byte()
        Dim inValue As SendMessageRequest = New SendMessageRequest()
        inValue.Body = New SendMessageRequestBody()
        inValue.Body.userId = userId
        inValue.Body.passwordDigest = passwordDigest
        inValue.Body.nonce = nonce
        inValue.Body.ts = ts
        inValue.Body.message = message
        Dim retVal As SendMessageResponse = CType(Me, FwHL7Soap).SendMessage(inValue)
        Return retVal.Body.SendMessageResult
    End Function

    <WebMethod()>
    <System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)>
    Function FwHL7Soap_SendMessageAsync(ByVal request As SendMessageRequest) As System.Threading.Tasks.Task(Of SendMessageResponse) Implements FwHL7Soap.SendMessageAsync
        Return MyBase.Channel.SendMessageAsync(request)
    End Function

    <WebMethod()>
    Public Function SendMessageAsync(ByVal userId As String, ByVal passwordDigest() As Byte, ByVal nonce As String, ByVal ts As Date, ByVal message() As Byte) As System.Threading.Tasks.Task(Of SendMessageResponse)
        Dim inValue As SendMessageRequest = New SendMessageRequest()
        inValue.Body = New SendMessageRequestBody()
        inValue.Body.userId = userId
        inValue.Body.passwordDigest = passwordDigest
        inValue.Body.nonce = nonce
        inValue.Body.ts = ts
        inValue.Body.message = message
        Return CType(Me, FwHL7Soap).SendMessageAsync(inValue)
    End Function
End Class

我想发表评论以寻求澄清,但我的声誉还差 2 才能做到这一点。

相反,我会做一些假设。

假设 1 - 您无权访问生成 wsdl 的 Web 服务。

假设 2 - 您正在构建一个将调用 Web 服务的客户端,并将跟踪通过客户端进行的所有调用。

假设 3 - 您正在使用 Visual Studio。

如果我的假设不正确,请随时发表评论,我会编辑我的答案以更符合您的问题。

除此之外,从 wsdl 生成的所有 vb 代码都是不必要的,只会使您的项目膨胀。

相反,由于您已将 wsdl 添加为服务引用,因此您可以直接从应用程序中的方法调用 Web 服务。添加服务引用后,您的项目可以访问所有 Web 方法,就好像该服务是您添加的 class 一样。从您链接的 wsdl 来看,SendMessage 方法似乎是您要使用的方法。

Public Sub SendMyMessage()
    Dim password As New Byte 
    Dim userId As String = "MyUserId"
    Dim nonce As String = "I'm not sure what this is"
    Dim ts As Date = Date.Now 'I initialized this with the current date
    Dim message As New Byte 'This is your message
    Dim webservice As New FwHL7 'This would be the name of your web service reference
    Dim response = webservice.SendMessage(userId, password, nonce, ts, message) 'This is where you actually invoke the webservice and load its response into a variable
End Sub

从那里,只需添加代码来记录消息。