ASP.NET托管WCF服务,需要增加MaxStringContentLength,但如何?

时间:2013-02-11 23:53:42

标签: asp.net wcf maxstringcontentlength

我的ASP.NET服务器提供了一组由我的WPF客户端使用的WCF服务。一切都工作得很好,直到字符串字段的长度超过8K。现在,这会在ASP.NET服务器上生成以下异常...

  

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

我已经在WPF app.config上将 MaxStringContentLength 的值增加到64K,但这还没有解决问题。所以我想我也需要在ASP.NET端增加这个值。但我在web.config中没有任何值可以改变!这是我的web.config来显示这个......

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />

    <authentication mode="Forms">
      <forms name=".ASPXFTOAUTH"
             timeout="10"
             slidingExpiration="true"
             cookieless="UseCookies"/>
    </authentication>

    <membership>
      <providers>
        <clear/>
      </providers>
    </membership>

    <customErrors defaultRedirect="~/Error.htm" mode="RemoteOnly">
      <error statusCode="404" redirect="~/404.aspx" />
    </customErrors>
  </system.web>

  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
                               multipleSiteBindingsEnabled="true" />
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

那么如何更新服务器以指示更高的 MaxStringContentLength 值?我的服务的app.config看起来像这样......

<system.serviceModel>
  <bindings>
    <basicHttpBinding>
      <binding name="BasicHttpBinding_IAccess" closeTimeout="00:01:00"
               openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
               allowCookies="false" bypassProxyOnLocal="false"
               hostNameComparisonMode="StrongWildcard"
               maxBufferSize="131072" maxBufferPoolSize="524288" maxReceivedMessageSize="131072"
               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 binding="basicHttpBinding"
              bindingConfiguration="BasicHttpBinding_IAccess" 
              contract="AccessService.IAccess"
              name="BasicHttpBinding_IAccess" />
  </client>
</system.serviceModel>

有什么想法吗?

更新

我的服务是通过使用“Access.svc”类

来定义的
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Access : IAccess
{
    // ...IAccess method implementations
}

...具有以下标记......

<%@ ServiceHost Language="C#" Debug="true" 
                Service="Ecotech.AMS.WebServer.Access" 
                CodeBehind="Access.svc.cs" %>

...在web.config中没有关于服务的具体内容,如评论中所述。

2 个答案:

答案 0 :(得分:0)

点击&#34;添加服务参考&#34;通过项目的右键菜单。这样做会将必要的配置信息添加到web.config文件中。

执行此操作后,您可以在绑定上设置maxReceivedMessageSize属性。这样做将最准确地反映您在WCF服务方面所做的事情。

答案 1 :(得分:0)

您需要处理的地方是Web配置,您需要添加服务行为,您可以在其中设置数据大小。例如,像这样,

<behaviors>
      <serviceBehaviors>
        <behavior name="SilverlightWCFLargeDataApplication">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>

      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="SilverlightWCFLargeDataApplication">
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>

如果不起作用,请在此处发布您的网络配置。希望它有所帮助。

相关问题