尝试在wcf中发送文件时出错

时间:2011-07-08 02:01:53

标签: c# wcf file-transfer nettcpbinding

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

  

格式化程序抛出异常   试图反序列化消息:   反序列化请求主体时出错   操作'Send_File'的消息。该   最大数组长度配额(16384)具有   读取XML数据时已被超出。   这个配额可能会增加   更改MaxArrayLength属性   在XmlDictionaryReaderQuotas上   创建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>

我如何解决这个异常?

3 个答案:

答案 0 :(得分:1)

在指定服务行为和端点行为

时添加此项

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

<endpoint behaviorConfiguration = "myBehavior"/>

然后指定此行为:

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

在服务器上:

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

答案 1 :(得分:0)

您可能希望在您的情况下尝试流式传输模式。 http://msdn.microsoft.com/en-us/library/ms789010.aspx

答案 2 :(得分:0)

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

您的客户端有一个NetTcpBinding和一个DualWsHttpBinding,您的服务器有一个wsHttpBinding。老实说,我很惊讶他们甚至可以互相沟通。 (除非您使用的是WCF 4.0,否则您将拥有默认绑定和端点)。

其次,您的服务配置文件未引用delared绑定 - 端点上没有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>