WCF 无法反序列化字节数组

WCF fails to deserialize byte array

MySettingsViewModel class 只有 3 个字段。当应用程序执行 WCF 调用它命中 WCF 服务,正确获取数据。但是在收到对象后,Web 客户端(Asp.NET MVC Web 应用程序)抛出以下异常。

接收 WCF 响应时 Webcontroller 出错:

The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:MySettingsResult. The InnerException message was 'Error in line 1 position 647. 'EndElement' 'MySettingsResult' from namespace 'http://tempuri.org/' is not expected. Expecting element '_x003C_Concurrency_x003E_k__BackingField'.'. Please see InnerException for more details.

  [Serializable]    
    public class MySettingsViewModel
    {

        public int DefaultCurrencyID { get; set; }

        public string TimezoneID { get; set; }


        public byte[] Concurrency { get; set; }
    }

所以似乎并发字节数组的反序列化在这里给出了问题。当我删除 [Serializable] 属性时,代码工作正常。但是我需要它是可序列化的,因为我需要将它发送到 Redis 缓存。

这是我在 Web 客户端中的 WCF 设置,

<system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="ExtendedMaxSize" maxReceivedMessageSize="999999999">
          <readerQuotas maxDepth="32" maxStringContentLength="5242880" maxArrayLength="2147483646" maxBytesPerRead="4096" maxNameTableCharCount="5242880"/>
        </binding>
      </wsHttpBinding>
      <basicHttpBinding>
        <binding name="" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
        </binding>           
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>   
    <client>
      <endpoint address="http://localhost:1800/MyAppManagement.svc" binding="wsHttpBinding" bindingConfiguration="ExtendedMaxSize" contract="MyApp.IAppManagement" name="WSHttpBinding_IApplicationManagement"/>
      ...some services
    </client>
    <diagnostics>
      <messageLogging logMessagesAtTransportLevel="true" logMessagesAtServiceLevel="false" logMalformedMessages="true" logEntireMessage="true" maxSizeOfMessageToLog="65535000" maxMessagesToLog="500"/>
    </diagnostics>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>

WCF 设置(服务端),

<system.serviceModel>
    <bindings>     
     <wsHttpBinding>
        <binding name="ExtendedMaxSize" maxReceivedMessageSize="999999999">
          <readerQuotas maxDepth="32" maxStringContentLength="5242880" maxArrayLength="2147483646" maxBytesPerRead="4096" maxNameTableCharCount="5242880"/>
        </binding>
      </wsHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>             
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />              
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="MyApp.Services.MyAppManagement">
        <endpoint address="" binding="wsHttpBinding" bindingConfiguration="ExtendedMaxSize" contract="MyApp.IAppManagement" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
        Other services...
    </services>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

解决了问题。将 [DataMember] 添加到 "public byte[] Concurrency { get; set; }" 使其工作。