首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WCF + REST增加MaxStringContentLength

WCF + REST增加MaxStringContentLength
EN

Stack Overflow用户
提问于 2011-08-10 15:45:02
回答 1查看 2.7K关注 0票数 3

我们遇到以下错误:

反序列化Project.ModelType类型的对象时出错。读取XML数据时已超出最大字符串内容长度配额(8192)。可以通过更改在创建XML读取器时使用的MaxStringContentLength对象上的XmlDictionaryReaderQuotas属性来增加此配额。

有大量的文章,论坛帖子等,展示了如何增加MaxStringContentLength的服务规模。我遇到的问题是,所有这些示例都使用绑定,而我们不使用绑定。我们没有在服务项目的web.config中设置绑定或端点配置。我们使用的是.cs文件,而不是.svc文件。我们已经实现了RESTful WCF服务。

在客户端,我们使用WebChannelFactory调用我们的服务。

ASP.NET 4.0

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-10 16:53:30

您确实有一个绑定,只是WebChannelFactory是自动为您设置的。事实证明,这个工厂总是用WebHttpBinding创建一个端点,因此您可以在创建第一个通道之前更改绑定属性--参见下面的示例。

代码语言:javascript
复制
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();
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7013700

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档