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

本文关键字:MaxStringContentLength 8192字节 反序列化 对象 WCF 服务 | 更新日期: 2023-09-27 18:06:00

我创建了一个简单的WCF web服务,它有一个方法:SubmitTicket(flightticket ft,字符串用户名,字符串密码)

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

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

"时使用。

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

我已经附上了我的客户端和服务器配置代码(因为它是在此刻,它已经经历了很多很多的变化在一天没有成功)。

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


这里是适用的app.config/web。定义端点绑定的配置代码:

& lt; & lt; & lt; & lt; & lt;客户端>>>>>

<?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;服务器>>>>>

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

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

我认为问题是您的服务没有拾取其配置,因为您已将服务名称设置为FlightTicketWebService,而我猜想实际类型是在名称空间中。使用名称空间完全限定服务名称,它应该拾取您的config

本质上这是WCF 4默认端点功能的副产品如果它没有找到匹配的配置它会将端点与默认配置

这就是答案!我在WCF 4.0中到处搜索这个问题的解决方案,Richard Blewett的这篇文章是这个谜题的最后一块。

从我的研究中学到的关键东西:

  • 如果异常是由服务抛出的,那么只更改服务器网络。配置文件;不用担心客户端
  • 创建一个自定义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>
相关文章:
  • 没有找到相关文章