如何将WSDL实现到ASMX

时间:2017-05-02 19:38:21

标签: asp.net vb.net web-services soap wsdl

我有这个WSDL,它应该用于IIS服务器中的入站/出站消息。我使用" 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

1 个答案:

答案 0 :(得分:2)

我评论要求澄清,但我没有能够做到这一点的声誉。

相反,我会做出一些假设。

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

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

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

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

有了这个,wsdl生成的所有vb代码都是不必要的,只会使你的项目膨胀。

相反,由于您已将wsdl添加为服务引用,因此可以直接从应用程序中的方法调用Web服务。添加服务引用后,项目可以访问所有Web方法,就好像该服务是您添加的类一样。从您链接的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

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