WCF, Streaming, Message Contract Error: Error in d

2020-03-26 07:38发布

问题:

I have developed a bit of a complicated WCF Service method. I would like to use the Streaming transfer mode, and because I have more than one parameter, I have defined a MessageContract with a body and a header.

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

    [MessageHeader]
    public string FileName;

    [MessageHeader]
    public int FileLengthInBytes;

    [MessageHeader]
    public int LengthInSeconds;

    [MessageBodyMember]
    public Stream ReportAudio;
}

Notice the stream is the only member of the body, per guidelines I read on MSDN.

The method is defined as such:

    [OperationContract]
    void SaveReportAudio(ReportAudioMessage reportToSave);

When I attempt to Invoke the method (using reflection), I get an error:

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 is the name of the Service Method that I am calling. ReportAudioMessage is the name of the MessageContract that is defined. Clearly, my Soap Message is getting jacked up, but I don't know how... :(

The following is the Service Model node, of the Service's web config:

<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>

And here is the Service Model node of the client's 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>

回答1:

I was able to get this working, though I'm not happy with the solution. I feel like it is a hack, partly because I don't understand it completely.

After pondering the error, I decided to enable WCF tracing and see what I could find. I saw the Xml message and noticed that indeed the element had the name "ReportAudioMessage". So, I decided to modify the message contract, and set the WrapperName:

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

Notice the "WrapperName" property. Now, that is the name of the method on my WCF service. It now works. However, it is frustrating me because this works now on two methods - a save method and a get method:

[OperationContract]
void SaveReportAudio(ReportAudioMessage message);

[OperationContract]
ReportAudioMessage GetReportAudio(GetReportAudioRequestMessage request);

Anyhow, it is working for me, so I just am going with it. I would appreciate any further comments or advice. Thanks,