WCF无法反序列化字节数组

时间:2015-07-16 11:49:48

标签: asp.net-mvc wcf serialization

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

接收WCF响应时Web控制器出错:

  

格式化程序在尝试反序列化时抛出异常   消息:尝试反序列化参数时出错   http://tempuri.org/:MySettingsResult。该   InnerException消息是'第1行中的错误位置647.'EndElement'   来自命名空间的'MySettingsResult'   不期望'http://tempuri.org/'。期待元素   '_x003C_Concurrency_x003E_k__BackingField'。'。请参阅   InnerException以获取更多详细信息。

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

1 个答案:

答案 0 :(得分:0)

排除问题。将[DataMember]添加到“public byte [] Concurrency {get; set;}”使其正常工作。