【问题标题】:The maximum string content length exceeded while reading XML data读取 XML 数据时超出最大字符串内容长度
【发布时间】:2013-11-26 15:05:39
【问题描述】:

我正在实现一个分布式处理系统,其中客户端发送请求,参数是一个对象列表(序列化)和另一个双值。到服务器,服务器返回一个对象列表作为结果。我实现了服务器和客户端部分。

客户端部分:

BasicHttpBinding myBinding = new BasicHttpBinding();
myBinding.ReaderQuotas.MaxStringContentLength = Int32.MaxValue;
myBinding.MaxBufferSize = Int32.MaxValue;
myBinding.MaxReceivedMessageSize = Int32.MaxValue;
myBinding.ReaderQuotas.MaxArrayLength = Int32.MaxValue;

var myEndPoint = new EndpointAddress("http://172.20.54.168:6525/ServerObject");
var myChannelFactory = new ChannelFactory<MarchingCubesInterface>(myBinding, myEndPoint);
MarchingCubesInterface mc = null;
mc = myChannelFactory.CreateChannel();

在服务器端

BasicHttpBinding serviceBind = new BasicHttpBinding();
serviceBind.ReaderQuotas.MaxStringContentLength = Int32.MaxValue;
serviceBind.MaxBufferSize = Int32.MaxValue;
serviceBind.MaxReceivedMessageSize = Int32.MaxValue;
serviceBind.ReaderQuotas.MaxArrayLength = Int32.MaxValue;
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);

对象正在序列化,但如果我尝试发送超过 4 个元素的序列化列表,它会显示读取 XML 数据时超出了最大字符串内容长度。我纯粹在纯代码基础上实现这一点(没有单独的配置文件)。有什么想法我犯了错误吗?

【问题讨论】:

    标签: c# xml wcf


    【解决方案1】:

    我看不到您将代码中定义的绑定分配给服务的位置 - 如果您不这样做,那么您将获得 BasicHttpBinding 的默认值。 MaxStringContentLength 的默认值是 8192。尝试在您的服务代码中添加类似这样的内容:

    host.AddServiceEndpoint(typeof(MarchingCubesInterface), serviceBind, "http://172.20.54.168:6525/ServerObject");
    

    这将使用您在发布的代码中定义的绑定为服务添加一个具有指定地址的端点。

    【讨论】:

      【解决方案2】:

      问题可能在于您的服务器应用程序可能设置了硬限制。

      检查您的服务的web.config 文件,并将对应的MaxStringContentLength(默认为8192,或8KB)调整为您认为合适的值。

      这是一个类似的帖子:

      Sending object to WCF service. MaxStringContentLength (8192 bytes) exceeded when deserializing

      【讨论】:

      • 问题是我不知道这个 web.config 文件在哪里?我不是自己创造的。我也找不到。
      • Web.config 文件可以在您的 Web 应用程序的根目录中找到。
      • OP 表示他们没有在问题中使用任何配置文件。
      • 虽然 OP 提到他没有实现任何配置文件,但在预先存在的 web.config 文件上表示的默认值仍然可能影响服务器应用程序的行为。但我很高兴你能指出问题!
      猜你喜欢
      • 2012-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多