当前位置: 代码迷 >> Web Service >> WCF 方法参数字符长度超过8192出错,请求指导
  详细解决方案

WCF 方法参数字符长度超过8192出错,请求指导

热度:524   发布时间:2014-02-23 23:10:40.0
WCF 方法参数字符长度超过8192报错,请求指导。
格式化程序尝试对消息反序列化时引发异常: 对操作“GetData”的请求消息正文进行反序列化时出现错误。读取 XML 数据时,超出最大字符串内容长度配额 (8192)。通过更改在创建 XML 读取器时所使用的 XmlDictionaryReaderQuotas 对象的 MaxStringContentLength 属性,可增加此配额。 行 1,位置 10125。

一直出现这个错误,怎么修改都不正确。
网上也搜了下,找到的修改办法试了也不起作用。
下面是我根据网上找的一些内容做的一些修改,但是运行起来还是出问题,求指导。

运行环境,win7,vs2010,mvc3

服务器端代码:

namespace WcfService
{
    public class Service1 : IService1
    {
        public int GetData(string value)
        {
            value = value == null ? "" : value;
            return value.Length;
        }
    }
}


namespace WcfService
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        int GetData(string value);
    }

}



客户端代码:



Service1Client wcf_client = new Service1Client();
            string itemstr = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
            string teststr = "";
            for (int i = 0; i < 100; i++)
            {
                teststr += itemstr;
            }

            int b = teststr.Length;
            int a = wcf_client.GetData(teststr);
            wcf_client.Close();




服务器端配置:

<system.serviceModel>
 
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
        
        <behavior name="A">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
        
      </serviceBehaviors>
    </behaviors>

    <services>
      <service behaviorConfiguration="A" name="WcfService.IService1.GetData">
        <endpoint address="" bindingConfiguration="BBB" binding="basicHttpBinding" contract="WcfService.IService1.GetData"/>
      </service>
    </services>

    <bindings>
      <basicHttpBinding>
        <binding name="BBB" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <security mode="None"></security>
        </binding>
      </basicHttpBinding>
    </bindings>
    
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>


客户端配置:

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IService1" closeTimeout="00:01:00"
          openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"