远程服务器返回意外响应:(413)请求实体太大。?

时间:2015-06-17 11:27:51

标签: c# web-services wcf

我以字符串格式从.net应用程序传递一些值给我的WCF服务。传递字符串格式将在此结构中,

ItemName~ItemDescription~ItemPrice|ItemName~ItemDescription~ItemPrice|...

每个订单项将以“|”分隔字符。我传递了近1000件物品。它按预期工作,但当我传递1500个项目时,会发生此错误。

The remote server returned an unexpected response: (413) Request Entity Too Large.

请帮我解决此错误。

这是服务中的方法

private void InsertGpLineItems(string lineItems)
{
    //Here I will process the insertion of line items to the GP system.
}

这是我的WCF服务上的 web.config

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="connectionString" value="data source=localhost; initial catalog=TWO; integrated security=SSPI"/>
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <pages validateRequest="false" />
    <httpRuntime requestValidationMode="2.0" />
  </system.web>
  <system.diagnostics>    
    <sources>
      <source name="System.ServiceModel.MessageLogging"
              switchValue="Information, ActivityTracing, Error">
        <listeners>
          <add name="messages"
               type="System.Diagnostics.XmlWriterTraceListener"
               initializeData="messages.svclog" />
        </listeners>
      </source>
    </sources> 
  </system.diagnostics>
  <system.serviceModel>
    <diagnostics>
      <messageLogging logEntireMessage="true"
                      logMalformedMessages="true"
                      logMessagesAtServiceLevel="true"
                      logMessagesAtTransportLevel="true"
                      maxMessagesToLog="3000"
                      maxSizeOfMessageToLog="2000"/>
    </diagnostics>
    <services>
      <service name="Service1.IService1">
        <endpoint address="" binding="basicHttpBinding"
                  contract="Service1.IService1">
        </endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:50935/Service1.svc"/>
          </baseAddresses>
        </host>
        <!--<endpoint address="http://localhost:50935/Service1.svc" binding="basicHttpBinding"></endpoint>-->
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="SampleBinding" 
                 messageEncoding="Text"
                 closeTimeout="00:02:00"
                 openTimeout="00:02:00"
                 receiveTimeout="00:20:00"
                 sendTimeout="00:02:00"
                 allowCookies="false"
                 bypassProxyOnLocal="false"
                 hostNameComparisonMode="StrongWildcard"
                 maxBufferPoolSize="2147483647" 
                 maxBufferSize="2147483647"
                 maxReceivedMessageSize="2147483647"
                 textEncoding="utf-8"
                 transferMode="Buffered"
                 useDefaultWebProxy="true">          
          <readerQuotas maxDepth="2000000"
                        maxStringContentLength="2147483647"
                        maxArrayLength="2147483647"
                        maxBytesPerRead="2147483647"
                        maxNameTableCharCount="2147483647"  />
          <security mode="Transport">
            <transport clientCredentialType="None" 
                       proxyCredentialType="None" 
                       realm="">
            </transport>
          </security>
        </binding>        
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="behaviorGPLineItemsService">
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

3 个答案:

答案 0 :(得分:2)

试试这个,

<bindings>
  <basicHttpBinding>
    <binding maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text">
      <readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
    </binding>
  </basicHttpBinding>
</bindings>

代替代码中的这些行,

<bindings>
  <basicHttpBinding>
    <binding name="SampleBinding" 
             messageEncoding="Text"
             closeTimeout="00:02:00"
             openTimeout="00:02:00"
             receiveTimeout="00:20:00"
             sendTimeout="00:02:00"
             allowCookies="false"
             bypassProxyOnLocal="false"
             hostNameComparisonMode="StrongWildcard"
             maxBufferPoolSize="2147483647" 
             maxBufferSize="2147483647"
             maxReceivedMessageSize="2147483647"
             textEncoding="utf-8"
             transferMode="Buffered"
             useDefaultWebProxy="true">          
      <readerQuotas maxDepth="2000000"
                    maxStringContentLength="2147483647"
                    maxArrayLength="2147483647"
                    maxBytesPerRead="2147483647"
                    maxNameTableCharCount="2147483647"  />
      <security mode="Transport">
        <transport clientCredentialType="None" 
                   proxyCredentialType="None" 
                   realm="">
        </transport>
      </security>
    </binding>        
  </basicHttpBinding>
</bindings>

我希望这会对你有所帮助。 :)

答案 1 :(得分:1)

它可能无法正常工作的一个原因是您的端点未使用已定义的绑定,因为您未通过bindingConfiguration中的endpoint属性指定它元件。这会导致WCF使用basicHttpBinding的默认值(较低),而不是您的值。

试试这个:

<service name="Service1.IService1">
  <endpoint address="" 
            binding="basicHttpBinding"
            bindingConfiguration="SampleBinding"
            contract="Service1.IService1">
  </endpoint>
  <host>
    <baseAddresses>
      <add baseAddress="http://localhost:50935/Service1.svc"/>
    </baseAddresses>
  </host>
  <!--<endpoint address="http://localhost:50935/Service1.svc" binding="basicHttpBinding"></endpoint>-->
</service>

请注意上面示例中bindingConfiguration属性的使用。

另请注意,如果您在IIS中托管服务,则不需要baseAddress元素,因为基地址将是.svc文件的位置。

<强> ADDED

同样的原则也适用于您的端点行为 - 它没有被分配给端点 - 您需要使用behaviorConfiguration属性:

<service name="Service1.IService1">
  <endpoint address=""
            behaviorConfiguration="behaviorGPLineItemsService"
            binding="basicHttpBinding"
            bindingConfiguration="SampleBinding"
            contract="Service1.IService" />

服务行为配置部分没有指定name属性,因此将其视为默认服务行为,并应用于未明确设置a的所有服务(在该配置文件中) behaviorConfiguration名称。

空白名称=默认也适用于绑定配置和端点行为配置,IIRC。

答案 2 :(得分:0)

这是因为您的服务属于HTTP GET类型,其长度有限。

如果您发送的数据非常大,则必须改为使用HTTP POST

[WebInvoke(Method = "POST")]
private void InsertGpLineItems(string lineItems)

此外,您需要编辑web.config文件,因为find here.