简体   繁体   English

WCF + REST,增加MaxStringContentLength

[英]WCF + REST, Increase MaxStringContentLength

We are running into the following error:我们遇到以下错误:

There was an error deserializing the object of type Project.ModelType.反序列化 Project.ModelType 类型的 object 时出错。 The maximum string content length quota (8192) has been exceeded while reading XML data.读取 XML 数据时,已超出最大字符串内容长度配额 (8192)。 This quota may be increased by changing the MaxStringContentLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader.可以通过更改创建 XML 阅读器时使用的 XmlDictionaryReaderQuotas object 上的 MaxStringContentLength 属性来增加此配额。

There are a ton of articles, forum posts, etc., showing how to increase the MaxStringContentLength size for a WCF service.有大量文章、论坛帖子等,展示了如何增加 WCF 服务的MaxStringContentLength大小。 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.我们的服务项目的web.config中没有设置绑定或端点配置。 We are using.cs files, not.svc files.我们使用的是 .cs 文件,而不是 .svc 文件。 We have implemented RESTful WCF services.我们已经实现了 RESTful WCF 服务。

On the client side, we are using WebChannelFactory to call our services.在客户端,我们使用WebChannelFactory来调用我们的服务。

ASP.NET 4.0 ASP.NET 4.0

Any ideas?有任何想法吗?

You do have a binding, it's just that the WebChannelFactory is setting it up for you automatically.您确实有一个绑定,只是WebChannelFactory会自动为您设置它。 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.事实证明,这个工厂总是使用WebHttpBinding创建一个端点,因此您可以在从中创建第一个通道之前更改绑定属性 - 请参见下面的示例。

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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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