尝试在wcf中发送文件时的错误

本文关键字:文件 错误 wcf | 更新日期: 2023-09-27 18:01:35

当我尝试使用WCF服务发送文件时,我得到这个异常

格式化程序抛出异常试图对消息进行反序列化:反序列化请求体时出错Send_File操作的消息。的最大阵列长度配额(16384)有在读取XML数据时超过。这个配额可以增加改变MaxArrayLength属性xmldictionaryreaderquota创建XML时使用的读者。

在发送之前,我首先将文件转换为字节数组这是发送文件

的客户端配置
<system.serviceModel>
<bindings>
  <netTcpBinding>
    <binding name="TcpBinding" closeTimeout="10:00:00" openTimeout="10:00:00"
        receiveTimeout="10:00:00" sendTimeout="10:00:00" transactionFlow="false"
        transferMode="Buffered" transactionProtocol="OleTransactions"
        hostNameComparisonMode="StrongWildcard" listenBacklog="10"
        maxBufferPoolSize="10000000" maxBufferSize="10000000" maxConnections="30"
        maxReceivedMessageSize="10000000">
      <readerQuotas maxDepth="64" maxStringContentLength="10000000" maxArrayLength="100000000"
          maxBytesPerRead="10000000" maxNameTableCharCount="10000000" />
      <reliableSession ordered="true" inactivityTimeout="10:00:00"
          enabled="false" />
    </binding>
  </netTcpBinding>
  <wsDualHttpBinding>
    <binding name="HttpBinding" closeTimeout="10:00:00" openTimeout="10:00:00"
        receiveTimeout="10:00:00" sendTimeout="10:00:00" bypassProxyOnLocal="false"
        transactionFlow="false" hostNameComparisonMode="StrongWildcard"
        maxBufferPoolSize="10000000" maxReceivedMessageSize="10000000"
        messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true">
      <readerQuotas maxDepth="32" maxStringContentLength="10000000" maxArrayLength="10000000"
          maxBytesPerRead="10000000" maxNameTableCharCount="10000000" />
      <reliableSession ordered="true" inactivityTimeout="10:00:00" />
    </binding>
  </wsDualHttpBinding>
</bindings>
<behaviors>
  <endpointBehaviors>
    <behavior name="filebehavior">
      <dataContractSerializer maxItemsInObjectGraph="2000000000"/>
    </behavior>
  </endpointBehaviors>
</behaviors>
<client>
  <endpoint address="net.tcp://localhost:8000/ChatRoom/service" behaviorConfiguration="filebehavior"
      binding="netTcpBinding" bindingConfiguration="TcpBinding"
      contract="ChatRoom" name="TcpBinding">
    <identity>
      <servicePrincipalName value="my_machine'ASPNET" />
    </identity>
  </endpoint>
  <endpoint address="http://localhost:8001/ChatRoom/service" binding="wsDualHttpBinding"
      bindingConfiguration="HttpBinding" contract="ChatRoom" name="HttpBinding">
    <identity>
      <servicePrincipalName value="my_machine'ASPNET" />
    </identity>
  </endpoint>
</client>

服务器配置

<system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="securingWSHttpBinding">
        </binding>
        <binding name="wsHttpBinding_ChatRoomServices" maxReceivedMessageSize="10000000" />
      </wsHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <serviceTimeouts transactionTimeout="10:00:00"/>
          <serviceMetadata httpGetEnabled="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
        <services>
      <service name="ChatRoomService.ChatRoom"
               behaviorConfiguration="ServiceBehavior">
        <endpoint address="service" binding="netTcpBinding" contract="ChatRoomService.IChatRoom" name="TcpBinding"/>
        <endpoint address="service" binding="wsDualHttpBinding" contract="ChatRoomService.IChatRoom" name="HttpBinding"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" name="MexBinding"/>
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8000/ChatRoom/"/>
            <add baseAddress="http://localhost:8001/ChatRoom/"/>
          </baseAddresses>
        </host>
      </service>
        </services>
    </system.serviceModel>

尝试在wcf中发送文件时的错误

在指定服务行为和端点行为时添加

在客户端,当您指定端点时,设置行为名称:

<endpoint behaviorConfiguration = "myBehavior"/>

,然后指定这个行为:

<behaviours>
    <endpointBehaviors>
        <behavior name="myBehavior">
            <dataContractSerializer maxItemsInObjectGraph="a number that is big enough"/>
        </behavior>
    </endpointBehaviors>
</behaviors>

在服务器上:

当你指定'service'和'endpoint'时,分别附加一个serviceBehavior和endpointBehavior,就像客户端一样。

首先,您的绑定甚至不匹配客户端和服务器之间:)

你的客户端有一个netttcpbinding和一个DualWsHttpBinding,你的服务器有一个wsHttpBinding。说实话,我很惊讶他们居然还能互相交流。(除非您使用的是WCF 4.0,在这种情况下,您将拥有默认绑定和端点)。

其次,你的服务配置文件没有引用声明的绑定——端点上没有bindingConfiguration属性,所以如果建立了通信,通道将使用指定绑定协议的默认值。

客户端配置看起来不错,尝试在服务器上这样做(我只包括NetTcpBinding协议,并向端点添加behaviorName和bindingConfiguration属性-其他绑定将类似):

<system.serviceModel>
  <bindings>
    <netTcpBinding>
      <binding name="TcpBinding" closeTimeout="10:00:00" openTimeout="10:00:00" receiveTimeout="10:00:00" sendTimeout="10:00:00" transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="10000000" maxBufferSize="10000000" maxConnections="30" maxReceivedMessageSize="10000000">
        <readerQuotas maxDepth="64" maxStringContentLength="10000000" maxArrayLength="100000000" maxBytesPerRead="10000000" maxNameTableCharCount="10000000" />       
        <reliableSession ordered="true" inactivityTimeout="10:00:00" enabled="false" />     
      </binding>
    </netTcpBinding>
  </bindings> 
  <behaviors>
    <endpointBehaviors>
      <behavior name="filebehavior">       
        <dataContractSerializer maxItemsInObjectGraph="2000000000"/>     
      </behavior>
    </endpointBehaviors> 
  </behaviors> 
  <service>
    <endpoint address="net.tcp://localhost:8000/ChatRoom/service" behaviorConfiguration="filebehavior" binding="netTcpBinding" bindingConfiguration="TcpBinding" contract="ChatRoom" name="TcpBinding">
      <identity>
        <servicePrincipalName value="my_machine'ASPNET" />
      </identity>
    </endpoint>    
  </service>
</system.serviceModel>

您可能需要尝试流式传输模式。http://msdn.microsoft.com/en-us/library/ms789010.aspx