将对象发送到WCF服务。反序列化

问题描述:

时MaxStringContentLength(8192个字节)超过了我创造出了一个方法的简单的WCF Web服务:SubmitTicket(flightticket英尺,用户名字符串,字符串密码)将对象发送到WCF服务。反序列化

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

“对flightticket类型的对象进行反序列化时出错,读取XML数据时超出了最大字符串内容长度配额(8192)。通过改变创建XML阅读器时使用的XmlDictionaryReaderQuotas对象的MaxStringContentLength属性“

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

我已经附加了我的客户端和服务器配置代码(因为目前它已经经历了很多很多很多改变,没有成功)。

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


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

< < < < < CLIENT >>>>>

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

我认为问题在于你的服务没有选择它的配置,因为你已经将服务名称设置为FlightTicketWebService,而我猜测实际类型是在命名空间中。与命名空间完全限定服务名称,它应该拿起你的配置

本质上讲,这是一个副产品的WCF 4默认端点的功能,如果发现没有匹配的配置它把终点了,默认的配置

+2

我设法弄清楚这个问题 - 我只是拿出绑定名称的服务器配置( deadcom

+0

我仍然具有指定为FlightTicketWebService的服务名称,尽管它不是完全限定名称,但它仍可以使用。 – deadcom

+0

那么它和你所做的改变默认的绑定配置(这就是使用没有名字的那个)是一样的。您的服务元素和您指定的端点将被忽略。基于http的基地址的默认值是使用BasicHttpBinding,这就是为什么你看到你的行为 –

这就是答案!我已经在WCF 4.0中找到了解决这个问题的办法,Richard Blewett撰写的这篇文章是这个难题的最后一部分。

从我的研究中取得的关键的东西:

  • 如果异常是由服务抛出,那么只有改变 服务器Web。配置文件;不用担心客户
  • 创建自定义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> 
  • 创建一个服务 ry,端点映射到自定义绑定。当端点的bindingConfiguration是一样的约束力的映射情况:
  • 确保服务名称合同值是完全合格的 - 使用的命名空间和类的名称。
<system.serviceModel> 
    <services> 
     <service name="Namespace.ServiceClassName"> 
      <endpoint 
       address="http://urlOfYourService" 
       bindingConfiguration="customBindingNameForLargeMessages"      
       contract="Namespace.ServiceInterfaceName" 
       binding="basicHttpBinding" 
       name="BasicHTTPEndpoint" /> 
     </service> 
    </services>