WCF 错误 413 请求实体太 la

WCF error 413 Request Entity too la

我最近有一个新的异常。当我尝试调用参数中包含字节数组的方法时,出现 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>

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

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

我看到很多关于这个错误的东西,但我尝试的任何东西都不起作用。

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

我尝试添加绑定并修改我的端点但是现在当我执行我的应用程序时我有一个:"The requested service can't be activated"

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

谢谢

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

发布的服务配置未指定任何绑定配置,因此 WCF 将使用指定值的默认设置。您需要 a) 定义要使用的绑定配置,以及 b) 将它们分配给端点。如果两者都不做,将导致 WCF 仍使用默认值。

在您的服务配置中,在 <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>

注意basicHttpBindingwebHttpBinding都需要指定配置。

接下来,通过 <endpoint> 元素上的 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 错误,您还需要调整 <httpRuntime> 元素中的 maxRequestLength。这在 <system.webServer> 部分的配置文件中:

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

maxRequestLength 的默认值是 4 MB,但问题很可能与用于您的服务绑定的值有关。

解决方案有效! 我添加了绑定,但我更改了 IMetaDataExchange 并写入 BXSportWCFLib.ServiceBX,但我没有消息 "The requested service can't be activated"

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