何处将绑定信息放在WCF Web.config中

时间:2015-11-18 11:47:20

标签: c# wcf

我收到错误“远程服务器返回了意外的响应:(413)请求实体太大。

我的Web.Config文件XML

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false"/>
          <!-- 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>
    </behaviors>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

请指导我如何增加内存大小以及缓冲区,消息接收和发送大小。

1 个答案:

答案 0 :(得分:0)

首先,我们必须在WCF服务的Web.config的ServiceModel中定义绑定

<bindings>
  <basicHttpBinding>
    <binding name="PowerTransmissionBinding" closeTimeout="01:00:00" openTimeout="01:00:00" receiveTimeout="01:00:00" sendTimeout="01:00:00"
             maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" transferMode="StreamedRequest" messageEncoding="Mtom" >
      <readerQuotas maxDepth="32" maxBytesPerRead="200000000"
       maxArrayLength="200000000" maxStringContentLength="200000000" />
    </binding>
  </basicHttpBinding>
</bindings>

然后我们必须定义EndPoint

<services>
  <service name="DataTestingWCF.Service1">
    <endpoint
       address="processData" binding="basicHttpBinding"
       bindingConfiguration="PowerTransmissionBinding"
       contract="DataTestingWCF.IService1"  />
  </service>
</services>

之后我们必须在Client Application App.config中建立这个新绑定

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IService1" sendTimeout="00:10:00"
                 messageEncoding="Mtom" transferMode="StreamedRequest" />
      </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:8099/Service1.svc/processData"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1"
            contract="SuperService.IService1" name="BasicHttpBinding_IService1" />
    </client>
</system.serviceModel>

通过这个我们可以实现Binding。

此处我是WCF Web.config XML的顶点

    <?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>

  <system.serviceModel>

    <bindings>
      <basicHttpBinding>
        <binding name="MybasicBinding" closeTimeout="01:00:00" openTimeout="01:00:00" receiveTimeout="01:00:00" sendTimeout="01:00:00"
                 maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" transferMode="StreamedRequest" messageEncoding="Mtom" >
          <readerQuotas maxDepth="32" maxBytesPerRead="200000000"
           maxArrayLength="200000000" maxStringContentLength="200000000" />
        </binding>
      </basicHttpBinding>
    </bindings>

    <services>
      <service name="DataTestingWCF.Service1">
        <endpoint
           address="myAddress" binding="basicHttpBinding"
           bindingConfiguration="MybasicBinding"
           contract="DataTestingWCF.IService1"  />
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false"/>
          <!-- 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>
    </behaviors>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>