我们遇到以下错误:
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/