asp.net - WCF + REST,增加 MaxStringContentLength

标签 asp.net wcf wcf-rest webchannelfactory

我们遇到以下错误:

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.

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

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

ASP.NET 4.0

有什么想法吗?

最佳答案

您确实有一个绑定(bind),只是 WebChannelFactory 会自动为您设置它。事实证明,该工厂始终使用 WebHttpBinding 创建端点,因此您可以在从中创建第一个 channel 之前更改绑定(bind)属性 - 请参阅下面的示例。

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

关于asp.net - WCF + REST,增加 MaxStringContentLength,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7013700/

相关文章:

c# - 使用 C# 动态更改 html 控件样式

c# - 为什么 "this"可以,但 "this"(在另一种方法中)不行?

wcf - IIS 6.0 阻止 WCF 4.0 Web 服务调用上的 "PUT"和 "DELETE"方法类型(403 禁止)

c# - 使用 WCF REST 服务进行基本身份验证的内置方法?

wcf - WCF WebApi 如何将请求 URI 映射到适当的服务类型/操作?

c# - 如何在IIS上产生负载?

c# - ASP.NET 3.5 onClientClick 返回 false 不工作

c# - 如何以编程方式登录共享点

c# - 死简单的 WCF - 405 方法不允许

c# - 有没有办法将 .xlsx 和 .xls 文件转换为 CODE 中的制表符分隔文本文件