2015-06-06 59 views
0

我在互联网上搜索了多个资源,但我遇到过的是'Hello World'和'计算器',就像解释WCF消息合约的示例一样。我想知道在现实世界的企业应用程序中消息合同的实际用法,以及何时应该优先于数据合同。任何有关这方面的帮助将不胜感激。WCF消息合同实际示例

+0

最好是设法实现它。 –

+0

https://msdn.microsoft.com/en-us/library/ms730255.aspx是一个非常真实的世界示例 –

+0

[DataContract vs Message Contract]的可能重复(http://stackoverflow.com/questions/4118654/datacontract -VS-消息合同) –

回答

0

例如,当一个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> 

然后服务器和客户端得到一个不匹配的错误,因为客户端无法找到方法。为了解决这个问题,你必须改变action,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>