使用JSON调用WCF REST服务时出错.超出长度配额(8192) [英] Error calling a WCF REST service using JSON. length quota (8192) exceeded

查看:52
本文介绍了使用JSON调用WCF REST服务时出错.超出长度配额(8192)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用.NET 4 RC在IIS中托管了WCF REST服务.使用JSON对服务的POST调用进行序列化.一切正常,直到其中一个DataMember(字符串)的大小超过8K为止.在这种情况下,我收到下面描述的错误,指示已超出MaxStringContentLength.端点的maxStringContentLength属性已增加,可以从配置文件中正确读取.

I’m having a WCF REST service hosted in IIS using .NET 4 RC. The POST calls to the service are serialized using JSON. Everything works fine until the size of one of the DataMember (string) is longer than 8K. In this case I receive the error described below indicating the MaxStringContentLength has been exceeded. The maxStringContentLength attribute for the endPoint has been increased and it is correctly read from the config file.

Web配置为:

<services>
  <service name="MyServiceServer" >
    <endpoint address="http://localhost/MyService" kind="webHttpEndpoint" endpointConfiguration="serviceEndPoint" contract="IMyService">
    </endpoint>
  </service>
</services>

<standardEndpoints>
  <webHttpEndpoint>
    <standardEndpoint name="serviceEndPoint" maxReceivedMessageSize="2048000"  maxBufferSize="2048000" maxBufferPoolSize="0">
      <readerQuotas maxStringContentLength="2048000" maxArrayLength="2048000"  maxDepth ="65000"/>
      <security mode="None">
        <transport clientCredentialType="None"/>
      </security>
    </standardEndpoint>
  </webHttpEndpoint>
</standardEndpoints>

IMyService接口定义为:

IMyService interface is defined as:

public interface IMyService
{
    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "/request", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
    void MyMehod(<Class Type> obj);
}

完整的错误消息是:

服务器在处理请求时遇到错误.异常消息是在反序列化type对象时发生错误.读取XML数据时,已超出最大字符串内容长度配额(8192).可以通过更改在创建XML阅读器时使用的XmlDictionaryReaderQuotas对象上的MaxStringContentLength属性来增加此配额.有关更多详细信息,请参见服务器日志.异常堆栈跟踪为:在System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject(XmlDictionaryReadName)的System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator读取器,布尔verifyObjectName,DataContractResolver dataContractResolver)处. System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(消息消息,Object []参数)在System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(消息消息,Object []中的Dispatcher.SingleBodyParameterMessageFormatter.DeserializeRequest(消息,Object []参数)参数)在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc)在System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(个人电脑rpc),位于System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc),位于System.ServiceModel.Dispatcher.MessageRpc.Process(布尔值isOperationContextSet)"

"The server encountered an error processing the request. The exception message is 'There was an error deserializing the object of type . The maximum string content length quota (8192) has been exceeded while reading XML data. This quota may be increased by changing the MaxStringContentLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader.'. See server logs for more details. The exception stack trace is: at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver) at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject(XmlDictionaryReader reader, Boolean verifyObjectName) at System.ServiceModel.Dispatcher.SingleBodyParameterMessageFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)"

推荐答案

这可行,只需确保使用完整的绝对URL作为端点地址即可.如果您变得狡猾并尝试使用相对路径,或者如果您省略.svc,则一旦请求太大,它将以奇怪的读者配额错误轰炸-

This works, just make sure to have a full absolute URL as your endpoint address. If you get crafty and try to use a relative path, or if you omit .svc it will bomb with the strange reader quota error once the request gets too large --

我会将此问题归为WCF,因为:

I would file this under a Bug for WCF because either:

  1. 相对URL应被禁止(并抛出适当的异常)

  1. 读者配额也应适用于相对路径

这篇关于使用JSON调用WCF REST服务时出错.超出长度配额(8192)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆