WCF错误413请求实体太啦

时间:2016-03-17 21:05:19

标签: c# wcf request web-config

我最近有一个新例外。当我尝试在参数中调用带有字节数组的方法时,会出现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>

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

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

我尝试添加绑定并修改我的端点,但现在当我执行我的应用程序时,我有一个:&#34;所请求的服务无法激活&#34;

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

由于

2 个答案:

答案 0 :(得分:0)

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

服务的已发布配置未指定任何绑定配置,因此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指定配置。

接下来,通过bindingConfiguration元素上的<endpoint>属性将它们分配到正确的端点:

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

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

maxRequestLength的默认值为4 MB,但最有可能的问题是用于服务绑定的值。

答案 1 :(得分:0)

解决方案的工作! 我添加了绑定,但我更改了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>