【问题标题】:ASP.NET hosting WCF Services, needs to increase MaxStringContentLength, but how?ASP.NET 托管 WCF 服务,需要增加 MaxStringContentLength,但是怎么做呢?
【发布时间】:2013-01-27 04:55:44
【问题描述】:

我的 ASP.NET 服务器正在提供一组 WCF 服务,这些服务由我的 WPF 客户端使用。一切正常,直到字符串字段的长度超过 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" %>

...如 cmets 中所述,web.config 中没有具体的服务。

【问题讨论】:

  • 你一定漏掉了什么。那不能是你的整个 web.config 文件。您没有参考那里的服务。您是如何添加 ServiceReference 的?
  • 不确定,所以我不将此作为答案发布,但唯一低于 8K 限制的值是 maxBytesPerRead。并且 MSDN 声明 A positive integer that specifies the maximum allowed bytes returned per read. The default is 4096. 尝试增加这个值
  • @Steve,他也需要在客户端设置这个值,但是配置文件甚至没有对服务的引用。
  • @ShaiCohen,web.config 中缺少整个部分,但 OP 声明该服务运行良好。所以我假设丢失的部分只是在此处粘贴 web.config 时丢失的东西。
  • 已更新以显示我的服务是如何添加的。 web.config 文件中根本没有绑定或其他提及服务。

标签: asp.net wcf maxstringcontentlength


【解决方案1】:

你需要处理的地方是web config,你需要添加可以设置数据大小的服务行为。比如这样,

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

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

【讨论】:

    【解决方案2】:

    尝试通过项目的右键菜单单击“添加服务引用”。这样做会将必要的配置信息添加到您的 web.config 文件中。

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

    【讨论】:

    • 右键单击我的 WPF 项目会提供“添加服务引用”选项,但右键单击 ASP.NET 项目不会。请注意,ASP.NET 是托管服务的地方,因此我认为向自身添加服务引用不会起作用。
    猜你喜欢
    • 1970-01-01
    • 2011-10-24
    • 1970-01-01
    • 1970-01-01
    • 2013-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-18
    相关资源
    最近更新 更多