我有一个可以接受
byte[]
的WCF服务。我正在使用HttpClient
创建客户端,并收到以下错误。我在网上阅读到,你必须在服务器和客户端都设置readerQuotas
,但是我该如何在HttpClient
上设置这些设置呢?
错误:
反序列化RTM.API.Resources.UGCRequest类型对象时发生错误。读取XML数据时已超过最大数组长度配额(16384)或对象图中的最大项数配额。可以通过更改XmlDictionaryReaderQuotas上的MaxArrayLength属性或MaxItemsInObjectGraph设置来增加这些配额。
服务器配置:
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647" maxBufferSize="2147483647"/>
<standardEndpoint name="DirectoryEndpoint"/>
</webHttpEndpoint>
</standardEndpoints>
<services>
<service name="API.Service.UGCService" behaviorConfiguration="DataServiceBehavior">
<endpoint contract="API.Service.UGCService" kind="webHttpEndpoint" endpointConfiguration="" binding="webHttpBinding" bindingConfiguration="BigHttpBinding"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="DataServiceBehavior">
<serviceDebug includeExceptionDetailInFaults="true"/>
<dataContractSerializer maxItemsInObjectGraph="2147483644"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="BigHttpBinding" transferMode="Buffered" maxReceivedMessageSize="2147483647" >
<readerQuotas maxArrayLength="2147483647" maxDepth="2147483647" maxStringContentLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
</binding>
</webHttpBinding>
</bindings>
客户端代码:
using (HttpClient client = new HttpClient(apiPath))
{
using (HttpRequestMessage request = new HttpRequestMessage(method, finalUrl))
{
request.Headers.Accept.AddString("application/json");
request.Headers.Add("Authorization", sb.ToString());
if (method == "POST" || method == "PUT")
{
if (requestBody.Count() == 0)
request.Headers.ContentLength = 0;
else
{
request.Content = HttpContent.Create(APM6.Utils.Serialize(requestBody), "application/json");
}
}
using (HttpResponseMessage response = client.Send(request))
{
return response.Content.ReadAsString();
}
}
}