【问题标题】:Sending object to WCF service. MaxStringContentLength (8192 bytes) exceeded when deserializing将对象发送到 WCF 服务。反序列化时超出 MaxStringContentLength(8192 字节)
【发布时间】:2011-10-28 05:54:37
【问题描述】:

我创建了一个简单的 WCF Web 服务,它有一个方法:SubmitTicket(flightticket ft, string username, string password)

在客户端,我有一个用于填写表格(机票)并将其发送到这个新创建的 Web 服务的应用程序。当此航班机票对象超过 8192 字节时,我收到以下错误:

“反序列化飞行票类型的对象时出错。读取 XML 数据时已超出最大字符串内容长度配额 (8192)。可通过更改创建XML 阅读器”

我在网上做了一些研究,发现我必须将 web.config(服务器)和 app.config(客户端)中的 MaxStringContentLength 设置为更高的数字。问题是,我已经通过阅读各种博客和网站尝试了两个配置文件中所有可能的设置组合,但它仍然因同样的错误而失败!

我已经附加了我的客户端和服务器配置代码(就目前而言,它在一天中经历了许多许多变化,但没有成功)。

我注意到的一件事是,当我更新服务引用时,我的客户端应用程序上的 configuration.svcinfo 文件似乎总是显示 MaxStringContentLength 为 8192。即使我明确设置了绑定属性,它似乎也采用了所有默认值。不确定这是否与我的问题有关,但值得一提。


这是用于定义端点绑定的适用 app.config/web.config 代码:

>>>>

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IFlightTicketWebService" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                useDefaultWebProxy="true">
                <readerQuotas maxDepth="32" maxStringContentLength="65536" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <security mode="None">
                    <transport clientCredentialType="None" proxyCredentialType="None"
                        realm="" />
                    <message clientCredentialType="UserName" algorithmSuite="Default" />
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://xx.xx.xx/xxxxxxxx.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IFlightTicketWebService"
            contract="FlightTicketWebService.IFlightTicketWebService"
            name="BasicHttpBinding_IFlightTicketWebService" />
    </client>
</system.serviceModel>
</configuration>

>>>>

<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
  <section name="GSH.FlightTicketWebService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web>
<httpRuntime maxRequestLength="16384"/>
<compilation debug="true" targetFramework="4.0"/>
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
</system.web>

<system.serviceModel>
  <bindings>
      <basicHttpBinding>
          <binding name="BasicHttpBinding_IFlightTicketWebService" closeTimeout="00:01:00"
              openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
              allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
              maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
              messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
              useDefaultWebProxy="true">
              <readerQuotas maxDepth="32" maxStringContentLength="65536" maxArrayLength="16384"
                  maxBytesPerRead="4096" maxNameTableCharCount="16384" />
              <security mode="None">
                  <transport clientCredentialType="None" proxyCredentialType="None"
                      realm="" />
                  <message clientCredentialType="UserName" algorithmSuite="Default" />
              </security>
          </binding>
      </basicHttpBinding>
  </bindings>
<behaviors>     
<serviceBehaviors>        
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
      <serviceMetadata httpGetEnabled="true"/>
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
<services>
    <service name="FlightTicketWebService">
        <endpoint 
            name="FlightTicketWebServiceBinding"
            address="http://xx.xx.xx/xxxxxxxxxxx.svc" 
            binding="basicHttpBinding" 
            bindingConfiguration="BasicHttpBinding_IFlightTicketWebService"
            contract="IFlightTicketWebService"/>
    </service>
</services>
</system.serviceModel>
<system.webServer>

【问题讨论】:

    标签: c# wcf xml-serialization wcf-binding maxstringcontentlength


    【解决方案1】:

    我认为问题在于您的服务没有获取其配置,因为您已将服务名称设置为 FlightTicketWebService 而我猜实际类型位于命名空间中。使用命名空间完全限定服务名称,它应该会选择您的配置

    本质上,这是 WCF 4 默认端点功能的副产品,如果找不到匹配的配置,它将使用默认配置放置端点

    【讨论】:

    • 我设法找出问题所在 - 我只是在服务器配置中取出绑定名称(
    • 我仍然将服务名称指定为 FlightTicketWebService,尽管它不是完全限定名称。
    • 嗯,这与您所做的相同,即更改默认绑定配置(这就是不使用名称所做的)。您指定的服务元素和端点将被忽略。基于 http 的基地址的默认设置是使用 BasicHttpBinding,这就是您看到自己行为的原因
    • 我已经与这个错误作斗争了 3 周(断断续续地)......真是一种解脱。不过,我认为这个 WCF 的东西可以变得更容易一些或有更好的文档!
    • 感谢您的帮助。不过,在尝试解决此问题时生成的配置的多次迭代中,我肯定使用完全限定名称指定了服务。奇怪的是它似乎从来没有对我有用。
    【解决方案2】:

    这就是答案!我在 WCF 4.0 中到处寻找解决这个问题的方法,Richard Blewett 的这篇文章是拼图的最后一块。

    从我的研究中学到的主要内容:

    • 如果服务抛出异常,则只更改 服务器 Web.config 文件;不用担心客户
    • 创建自定义basicHttpBinding
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="customBindingNameForLargeMessages">
    
    • 添加较大的 readerQuota 值(此处显示的最大可能值,根据口味调整)
            <binding name="customBindingNameForLargeMessages"
              maxReceivedMessageSize="2147483647">
              <readerQuotas maxDepth="2147483647"
                 maxStringContentLength="2147483647"
                 maxArrayLength="2147483647"
                 maxBytesPerRead="2147483647"
                 maxNameTableCharCount="2147483647" />
            </binding>
        </basicHttpBinding>
    </bindings>
    
    • 创建一个 service 条目,其中的 endpoint 映射到自定义 binding。当端点的 bindingConfiguration 与绑定的 name 相同时,就会发生映射:
    • 确保 service namecontract 值是完全限定的 - 使用命名空间和类的名称。
    <system.serviceModel>
        <services>
            <service name="Namespace.ServiceClassName">
                 <endpoint 
                     address="http://urlOfYourService"
                     bindingConfiguration="customBindingNameForLargeMessages"                     
                     contract="Namespace.ServiceInterfaceName" 
                     binding="basicHttpBinding"
                     name="BasicHTTPEndpoint" />
            </service>
        </services>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多