c# - WCF、流、消息约定错误 : Error in deserializing body of request message

标签 c# wcf wcf-streaming wcf-serialization

我开发了一些复杂的 WCF 服务方法。我想使用 Streaming 传输模式,因为我有多个参数,所以我定义了一个带有 body 和 header 的 MessageContract。

[MessageContract]
public class ReportAudioMessage
{
    [MessageHeader]
    public int ReportId;

    [MessageHeader]
    public string FileName;

    [MessageHeader]
    public int FileLengthInBytes;

    [MessageHeader]
    public int LengthInSeconds;

    [MessageBodyMember]
    public Stream ReportAudio;
}

请注意,根据我在 MSDN 上阅读的指南,流是主体的唯一成员。

方法定义如下:

    [OperationContract]
    void SaveReportAudio(ReportAudioMessage reportToSave);

当我尝试调用该方法(使用反射)时,出现错误:

Error in deserializing body of request message for operation 'SaveReportAudio'. OperationFormatter encountered an invalid Message body. Expected to find node type 'Element' with name 'SaveReportAudio' and namespace 'http://tempuri.org/'. Found node type 'Element' with name 'ReportAudioMessage' and namespace 'http://tempuri.org/'

SaveReportAudio 是我调用的服务方法的名称。 ReportAudioMessage 是定义的 MessageContract 的名称。显然,我的 Soap Message 被顶了起来,但我不知道如何...:(

以下是服务的网络配置的服务模型节点:

<system.serviceModel>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="false" />
    <bindings>
        <netTcpBinding>
            <binding
             name="VRManagerTcpBinding"
             closeTimeout="00:01:00"
             openTimeout="00:01:00"
             sendTimeout="00:01:00"
             receiveTimeout="00:01:00"
             transferMode="Streamed">
                <reliableSession enabled="false"/>
                <security mode="None" />
            </binding>
        </netTcpBinding>
    </bindings>
    <services>
        <service name="Radia.VoiceRecognition.Services.VRManager" behaviorConfiguration="VRManagerTcpBehavior">
            <host>
                <baseAddresses>
                    <add baseAddress="net.tcp://localhost:8011/VRManager"/>
                </baseAddresses>
            </host>
            <endpoint
             address="VRManager.svc"
             binding="netTcpBinding"
             bindingConfiguration="VRManagerTcpBinding"
             contract="Radia.VoiceRecognition.Services.IVRManager" />
            <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
        </service>
    </services>
    <behaviors>
       <serviceBehaviors>
        <behavior name="VRManagerTcpBehavior">
         <serviceMetadata httpGetEnabled="false" />
         <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior> 
       </serviceBehaviors>
      </behaviors>
</system.serviceModel>

这是客户端 App.Config 的服务模型节点:

<system.serviceModel>
<bindings>
  <netTcpBinding>
    <binding name="NetTcpBinding_IVRManager" closeTimeout="00:01:00"
      openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
      transactionFlow="false" transferMode="Streamed" transactionProtocol="OleTransactions"
      hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="524288"
      maxBufferSize="65536" maxConnections="10" maxReceivedMessageSize="65536">
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <reliableSession ordered="true" inactivityTimeout="00:10:00"
        enabled="false" />
      <security mode="None">
        <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
        <message clientCredentialType="Windows" />
      </security>
    </binding>
  </netTcpBinding>
</bindings>
<client>
  <endpoint address="net.tcp://xxxxxxxxxxx:8012/VRManager.svc/VRManager.svc"
    binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IVRManager"
    contract="VRManager.IVRManager" name="NetTcpBinding_IVRManager" />
</client>

最佳答案

虽然我对解决方案不满意,但我能够使它正常工作。我觉得这是一个 hack,部分原因是我不完全理解它。

思考错误后,我决定启用 WCF 跟踪并查看我能找到什么。我看到了 Xml 消息并注意到该元素确实具有名称“ReportAudioMessage”。所以,我决定修改消息契约,并设置 WrapperName:

[MessageContract(IsWrapped = true, WrapperName = "SaveReportAudio")]
public class ReportAudioMessage
{
    [MessageBodyMember]
    public Stream Session;
}

注意“WrapperName”属性。现在,这是我的 WCF 服务上的方法名称。现在可以了。然而,这让我很沮丧,因为这现在适用于两种方法 - 一种保存方法和一种获取方法:

[OperationContract]
void SaveReportAudio(ReportAudioMessage message);

[OperationContract]
ReportAudioMessage GetReportAudio(GetReportAudioRequestMessage request);

无论如何,它对我有用,所以我就用它。如果有任何进一步的意见或建议,我将不胜感激。谢谢,

关于c# - WCF、流、消息约定错误 : Error in deserializing body of request message,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22238820/

相关文章:

c# - 为什么 WCF 契约(Contract)会包含 ref/out 参数?

wcf - 具有WS-Security的WCF服务仅需要签名时间戳

wcf - 从服务器 : DTO or Wcf Data Services? 查询/保存的 'better' 方法是什么

c# - 流式wcf结束后如何删除文件

wcf - 使用 WCF 和 MTOM 进行流式传输

c# - 无法分配给 'textBoxSettingsMethod',因为它是 'method group'

C#相当于java的等待和通知?

c# - 删除或不写入行中的最后一个 ';' 符号

c# - Xamarin MvvM 内容 View 绑定(bind)