将对象发送到WCF服务。反序列化时超出MaxStringContentLength(8192字节)

时间:2011-08-16 16:43:33

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

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

在客户端,我有一个填写表格(机票)并将其发送到这个新创建的Web服务的申请。当这个flightticket对象超过8192bytes时,我收到以下错误:

"反序列化flightticket类型的对象时出错。读取XML数据时已超出最大字符串内容长度配额(8192)。通过更改创建XML阅读器时使用的XmlDictionaryReaderQuotas对象的MaxStringContentLength属性,可以增加此配额"

我在网上进行了一些研究,发现我必须将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>

&LT;&LT;&LT;&LT;&LT;服务器&gt;&gt;&gt;&gt;&gt;

<?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>

2 个答案:

答案 0 :(得分:4)

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

本质上,这是WCF 4默认端点功能的副产品,如果找不到匹配的配置,则会将端点设置为默认配置

答案 1 :(得分:1)

这就是答案!我已经在WCF 4.0中搜索了这个问题的所有解决方案,Richard Blewett的这篇文章是最后一部分。

从我的研究中学到的关键事项:

  • 如果服务抛出异常,则只更改     Server 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>
  • 创建服务条目,其端点映射到自定义绑定。当端点的 bindingConfiguration 与绑定的名称相同时,就会发生映射:
  • 确保服务名称合同值是完全限定的 - 使用命名空间和类的名称。
<system.serviceModel>
    <services>
        <service name="Namespace.ServiceClassName">
             <endpoint 
                 address="http://urlOfYourService"
                 bindingConfiguration="customBindingNameForLargeMessages"                     
                 contract="Namespace.ServiceInterfaceName" 
                 binding="basicHttpBinding"
                 name="BasicHTTPEndpoint" />
        </service>
    </services>