【问题标题】:WCF error 413 Request Entity too laWCF 错误 413 Request Entity too la
【发布时间】:2016-03-17 21:05:19
【问题描述】:

我最近有一个新的例外。当我尝试在参数中调用带有字节数组的方法时,出现 413 错误。 我尝试更改 maxBufferSize、maxReceivedMessageSize 和 maxBufferPoolSize,但没有任何改变。

在我的应用程序的 app.config 中,我有:

<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="soap" 
             maxReceivedMessageSize="1116553600"
             maxBufferPoolSize="1116553600"
             maxBufferSize="1116553600"/>
  </basicHttpBinding>
  <wsHttpBinding>
    <binding name="mex" maxReceivedMessageSize="1116553600"
             maxBufferPoolSize="1116553600">
      <security mode="None" />
    </binding>
  </wsHttpBinding>
</bindings>
<client>
  <endpoint address="http://localhost:9804/ServiceBX.svc/soap"
    binding="basicHttpBinding" bindingConfiguration="soap" contract="ServiceReferenceBX.ServiceBX"
    name="soap" />
</client>
</system.serviceModel>

在我的 WCF 服务的 web.config 中,我有:

<system.serviceModel>
<services>
  <service name="BXSportWCFLib.ServiceBX" behaviorConfiguration="MyServiceBehavior">
    <endpoint name="rest" address="" binding="webHttpBinding" contract="BXSportWCFLib.ServiceBX" behaviorConfiguration="restBehavior" />
    <endpoint name="mex" address="mex" binding="mexHttpBinding" contract="BXSportWCFLib.ServiceBX" />
    <endpoint name="soap" address="soap" binding="basicHttpBinding" contract="BXSportWCFLib.ServiceBX" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="MyServiceBehavior" >
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="restBehavior">
      <webHttp  />
    </behavior>
  </endpointBehaviors>
</behaviors>
</system.serviceModel>

我在这个错误上看到了很多东西,但我尝试的任何方法都不起作用。

首先我想知道我必须修改哪个文件?

我尝试添加绑定并修改我的 endoint,但现在当我执行我的应用程序时,我有一个:“请求的服务无法激活”

<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
<httpModules>
  <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
</httpModules>
</system.web>
<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="soap"
             maxReceivedMessageSize="116553600"
             maxBufferPoolSize="116553600"
             maxBufferSize="116553600">
      <readerQuotas maxStringContentLength="116533600" />
    </binding>
  </basicHttpBinding>
  <webHttpBinding>
    <binding name="web"
             maxReceivedMessageSize="116553600"
             maxBufferPoolSize="11653600"
             maxBufferSize="116553600">
      <readerQuotas maxStringContentLength="116533600" />
    </binding>
  </webHttpBinding>
</bindings>
<services>
  <service name="BXSportWCFLib.ServiceBX" behaviorConfiguration="MyServiceBehavior">
    <endpoint name="rest"
      address=""
      binding="webHttpBinding"
      bindingConfiguration="web"
      contract="BXSportWCFLib.ServiceBX"
      behaviorConfiguration="restBehavior" />
    <endpoint name="soap"
              address="soap"
              binding="basicHttpBinding"
              bindingConfiguration="soap"
              contract="BXSportWCFLib.ServiceBX" />
    <endpoint name="mex"
       address="mex"
       binding="mexHttpBinding"
       contract="IMetaDataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="MyServiceBehavior" >
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="restBehavior" >
      <webHttp  />
    </behavior>
  </endpointBehaviors>
</behaviors>
</system.serviceModel>

谢谢

【问题讨论】:

标签: c# wcf request web-config


【解决方案1】:

听起来您在调用服务时收到错误消息,因此您需要修复服务的配置文件,而不是客户端。

服务的发布配置未指定任何绑定配置,因此 WCF 将使用指定值的默认设置。您需要 a) 定义要使用的绑定配置,并 b) 将它们分配给端点。未能同时执行这两项操作将导致 WCF 仍使用默认值。

在您的服务配置中,在 &lt;system.serviceModel&gt; 部分添加以下内容:

<bindings>
  <basicHttpBinding>
    <binding name="soap"
             maxReceivedMessageSize="116553600"
             maxBufferPoolSize="116553600"
             maxBufferSize="116553600">
      <readerQuotas maxStringContentLength="116533600" />
    </binding>
  </basicHttpBinding>
  <webHttpBinding 
    <binding name="web"
             maxReceivedMessageSize="116553600"
             maxBufferPoolSize="11653600"
             maxBufferSize="116553600">
      <readerQuotas maxStringContentLength="116533600" />
    </binding>
  </webHttpBinding>
</bindings>

请注意,您需要为basicHttpBindingwebHttpBinding 指定配置。

接下来,通过&lt;endpoint&gt; 元素上的bindingConfiguration 属性将它们分配给正确的端点:

<endpoint name="rest" 
          address="" 
          binding="webHttpBinding" 
          bindingConfiguration="web"
          contract="BXSportWCFLib.ServiceBX"
          behaviorConfiguration="restBehavior" />
<endpoint name="soap" 
          address="soap" 
          binding="basicHttpBinding" 
          bindingConfiguration="soap"
          contract="BXSportWCFLib.ServiceBX" />

第三,您的 mex 端点指定了错误的合约 - 它应该是 IMetaDataExchange

<endpoint name="mex" 
           address="mex" 
           binding="mexHttpBinding" 
           contract="IMetaDataExchange" />

如果您仍然收到 413 错误,您还需要调整 &lt;httpRuntime&gt; 元素中的 maxRequestLength。这在 &lt;system.webServer&gt; 部分的配置文件中:

<system.web>
  <httpRuntime maxRequestLength="116533600" />
</system.web>

maxRequestLength 的默认值为 4 MB,但很可能问题出在服务绑定所使用的值上。

【讨论】:

  • 我尝试添加绑定并修改我的 endoint,但现在当我执行我的应用程序时,我有一个:“请求的服务无法激活”。我用这些信息更新了我的问题。我在事件查看器中看不到任何其他消息错误。
  • @Gobelet - 您可以在浏览器中浏览到服务地址吗?页面出现了吗?
  • 此解决方案最多可支持 10MB 数据,但是当我尝试上传 34MB 数据时,它会抛出“System.OutOfMemoryException”。有什么解决办法吗?
【解决方案2】:

解决方案有效! 我添加了绑定,但我更改了 IMetaDataExchange 并编写了 BXSportWCFLib.ServiceBX 并且没有消息“无法激活请求的服务”

<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
<httpModules>
  <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
</httpModules>
</system.web>
<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="soap"
             maxReceivedMessageSize="116553600"
             maxBufferPoolSize="116553600"
             maxBufferSize="116553600">
      <readerQuotas maxStringContentLength="116533600" />
    </binding>
  </basicHttpBinding>
  <webHttpBinding>
    <binding name="web"
             maxReceivedMessageSize="116553600"
             maxBufferPoolSize="11653600"
             maxBufferSize="116553600">
      <readerQuotas maxStringContentLength="116533600" />
    </binding>
  </webHttpBinding>
</bindings>
<services>
  <service name="BXSportWCFLib.ServiceBX" behaviorConfiguration="MyServiceBehavior">
    <endpoint name="rest"
      address=""
      binding="webHttpBinding"
      bindingConfiguration="web"
      contract="BXSportWCFLib.ServiceBX"
      behaviorConfiguration="restBehavior" />
    <endpoint name="soap"
              address="soap"
              binding="basicHttpBinding"
              bindingConfiguration="soap"
              contract="BXSportWCFLib.ServiceBX" />
    <endpoint name="mex"
       address="mex"
       binding="mexHttpBinding"
       contract="IMetaDataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="MyServiceBehavior" >
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="restBehavior" >
      <webHttp  />
    </behavior>
  </endpointBehaviors>
</behaviors>
</system.serviceModel>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多