在WCF Web.config中放置绑定信息的位置

本文关键字:绑定 信息 位置 WCF Web config | 更新日期: 2023-09-27 18:22:37

我收到错误"远程服务器返回意外响应:(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>

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

在WCF Web.config中放置绑定信息的位置

首先,我们必须在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>

然后我们必须定义端点

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

之后,我们必须在客户端应用程序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>

通过这个我们可以实现绑定。

在此,我将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>