【问题标题】:WCF Message Contract real-world exampleWCF 消息契约真实世界的例子
【发布时间】:2015-06-06 15:14:16
【问题描述】:

我在互联网上搜索了多个资源,但我所遇到的只是“Hello World”和“Calculator”之类的示例,这些示例解释了 WCF 中的消息契约。我想知道消息契约在现实世界的企业应用程序中的实际用法,以及何时应该优先使用它而不是数据契约。对此的任何帮助将不胜感激。

【问题讨论】:

标签: wcf


【解决方案1】:

例如,一个真实的场景是,当 Java 客户端有一个不会更改的 WSDL 文件(技术原因)时。所以Java客户端有一个固定的WSDL文件。然后 Web 服务 (.net) 必须必须为该方法提供准确的名称、命名空间、操作和消息字符串。

Java 客户端示例:

   <operation name="remove">
       <input wsam:Action="remove" message="tns:remove"/>
       <output wsam:Action="removeResponse" message="tns:removeResponse"/>
   </operation>

默认是在web服务中自动生成的,像这样:

<wsdl:operation name="remove">
  <wsdl:input message="tns:IService1_remove_InputMessage" wsaw:Action="http://tempuri.org/IService1/remove"/>
  <wsdl:output message="tns:IService1_remove_OutputMessage" wsaw:Action="http://tempuri.org/IService1/removeResponse"
</wsdl:operation>

然后服务器和客户端得到一个不匹配的错误,因为客户端找不到方法。要解决此问题,您必须更改操作、replyAction 和消息字符串。您必须更改的第一个和第二个:

<OperationContractAttribute(Action:="remove", name:="remove" ReplyAction:="removeResponse")> _
 Function remove(key As string) As Boolean

新的 WSDL 文件(服务器):

<wsdl:operation name="remove">
<wsdl:input message="tns:IService1_remove_InputMessage" wsaw:Action="remove"/>
<wsdl:output message="tns:IService1_remove_OutputMessage" wsaw:Action="removeResponse"
</wsdl:operation>

现在你仍然得到同样的不匹配错误,因为消息字符串不一样。要解决这个问题,您需要消息合同。这使您能够操作 WSDL 文件/SOAP 消息。 为此,该方法的语法在 IService 类中发生了变化。

<OperationContractAttribute(Action:="remove", name:="remove" ReplyAction:="removeResponse")> _
 Function remove(key As remove) As removeResponse

消息合约确定了“类型”:

 <MessageContract()> _
    Public Class removeResponse

    Private return1 As Boolean()

    <DataMember(Name:="return")> _
    Public Property returnP() As Boolean ()
        Get
            Return Me.return1
        End Get
        Set(ByVal value As Boolean ())
            Me.return1 = value
        End Set
    End Property

End Class

<MessageContract()> _
Public Class remove

    Private key1 As String()

    <DataMember(Name:="key")> _
    Public Property keyP() As String ()
        Get
            Return Me.key1
        End Get
        Set(ByVal value As String ())
            Me.key1 = value
        End Set
    End Property

End Class

现在客户端和服务器之间的通信正常。

WSDL(服务器):

<wsdl:operation name="remove">
<wsdl:input message="remove" wsaw:Action="remove"/>
<wsdl:output message="removeResponse" wsaw:Action="removeResponse"
</wsdl:operation>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-11
    • 2010-11-23
    • 1970-01-01
    • 2019-07-22
    • 2023-03-16
    相关资源
    最近更新 更多