【发布时间】:2012-06-19 13:43:59
【问题描述】:
我有一个可以接受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();
}
}
}
【问题讨论】:
-
作为一个题外话,我已经在 WCF 上苦苦挣扎 6 个月了,我不建议使用它来公开 JSON 接口(它有很多错综复杂不付出努力)。如果您想坚持使用 Microsoft 技术并可以评估替代方案,请查看 WebAPI。
标签: wcf wcf-client