【发布时间】:2026-01-07 21:20:03
【问题描述】:
当我尝试使用 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>
我该如何解决这个异常?
【问题讨论】:
-
您是否在客户端和/或服务中设置端点以使用上述绑定与端点元素中的 bindingConifguration 属性?如果不是,WCF 将使用绑定的默认值。
-
我添加了端点
标签: c# wcf file-transfer nettcpbinding