Error calling a WCF REST service using JSON. length quota (8192) exceeded
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 config is:
<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 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);
}
Complete Error Message is:
“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.SingleBodyPar开发者_JAVA百科ameterMessageFormatter.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)”
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 --
I would file this under a Bug for WCF because either:
- relative URLs should be disallowed (and an appropriate exception thrown)
or
- the reader quota should work with relative paths as well
Insert into your web.config:
<configuration>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingConfig">
<readerQuotas maxStringContentLength="2048000" />
</binding>
</webHttpBinding>
</bindings>
</system.serviceModel>
</configuration>
and insert attribute bindingConfiguration="webHttpBindingConfig" into your endpoint
I had similar problems but with .NET 3.5
I had no problems on the server log, so the problem was on the client. Seems that the configuration with the max values increased was not read and used...
So I solved passing the name of the endpoint EXPLICITLY in the constructor of the WebChannelFactory, using another overload.
WAS:
WebChannelFactory<IWKRestTest> factory = new WebChannelFactory<IWKRestTest>(new Uri(XXX));
factory.Credentials.UserName.UserName = K_USERNAME;
factory.Credentials.UserName.Password = K_PASSWORD;
IWKRestTest proxy = factory.CreateChannel();
IS:
WebChannelFactory<IWKRestTest> factory = new WebChannelFactory<IWKRestTest>("IWKRestTestService");
and in the app.config there's:
The Uri is indicated in the endpoint node but there you find also the bindingConfiguration and so on, so all the new increased limits now works.
精彩评论