简体   繁体   中英

WCF + REST, Increase MaxStringContentLength

We are running into the following error:

There was an error deserializing the object of type Project.ModelType. 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.

There are a ton of articles, forum posts, etc., showing how to increase the MaxStringContentLength size for a WCF service. The problem I'm experiencing is that all of these examples use Binding, which we do not use. We have no bindings or endpoint configurations set up in the web.config of our service project. We are using.cs files, not.svc files. We have implemented RESTful WCF services.

On the client side, we are using WebChannelFactory to call our services.

ASP.NET 4.0

Any ideas?

You do have a binding, it's just that the WebChannelFactory is setting it up for you automatically. It turns out that this factory always creates an endpoint with a WebHttpBinding , so you can change the binding properties before creating the first channel from it - see the example below.

public class StackOverflow_7013700
{
    [ServiceContract]
    public interface ITest
    {
        [OperationContract]
        string GetString(int size);
    }
    public class Service : ITest
    {
        public string GetString(int size)
        {
            return new string('r', size);
        }
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
        host.Open();
        Console.WriteLine("Host opened");

        WebChannelFactory<ITest> factory = new WebChannelFactory<ITest>(new Uri(baseAddress));
        (factory.Endpoint.Binding as WebHttpBinding).ReaderQuotas.MaxStringContentLength = 100000;
        ITest proxy = factory.CreateChannel();
        Console.WriteLine(proxy.GetString(100).Length);

        try
        {
            Console.WriteLine(proxy.GetString(60000).Length);
        }
        catch (Exception e)
        {
            Console.WriteLine("{0}: {1}", e.GetType().FullName, e.Message);
        }

        ((IClientChannel)proxy).Close();
        factory.Close();

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM