【问题标题】:WCF + REST, Increase MaxStringContentLengthWCF + REST,增加 MaxStringContentLength
【发布时间】:2011-10-24 05:31:18
【问题描述】:

我们遇到了以下错误:

反序列化类型对象时出错 项目.模型类型。最大字符串内容长度配额 (8192) 有 读取 XML 数据时超出。此配额可能会增加 更改 MaxStringContentLength 属性 创建 XML 阅读器时使用的 XmlDictionaryReaderQuotas 对象。

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

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

ASP.NET 4.0

有什么想法吗?

【问题讨论】:

  • 您能否向我们展示您的服务器端web.config 和客户端上用于创建代理的代码??
  • 错误出现在哪里?在客户端还是服务器?
  • 尝试反序列化对象时,错误出现在服务器端。

标签: asp.net wcf wcf-rest webchannelfactory


【解决方案1】:

您确实有绑定,只是WebChannelFactory 会自动为您设置。事实证明,这个工厂总是创建一个带有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();
    }
}

【讨论】:

    猜你喜欢
    • 2013-07-08
    • 2011-06-29
    • 2013-01-27
    • 1970-01-01
    • 2012-03-02
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    • 2013-09-21
    相关资源
    最近更新 更多