413实体太大异常,wcf服务

时间:2014-10-08 13:39:38

标签: wcf service web-config client app-config

我的代码因此例外而崩溃:

未处理的类型' System.ServiceModel.ProtocolException'发生在mscorlib.dll

其他信息:远程服务器返回了意外响应:(413)请求实体太大。

这是我的web.config文件:

<?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 name="WcfService1.Service1Behavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom"
                                    customUserNamePasswordValidatorType="WcfService1.CustomValidator, WcfService1"/>                                                            
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>  
    </behaviors>

    <services>
      <service name="WcfService1.Service1" behaviorConfiguration="WcfService1.Service1Behavior" >
        <endpoint address="wsHttp" binding="wsHttpBinding" contract="WcfService1.IService1"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses >
            <add  baseAddress="http://localhost/Service1.svc/wsHttp"/>
          </baseAddresses>
        </host>
      </service>
    </services>

    <bindings>       
      <wsHttpBinding>          
        <binding name="SafeServiceConf" maxReceivedMessageSize="2147483647">             
          <readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" />             
          <security mode="TransportWithMessageCredential">
            <message clientCredentialType="UserName"/>
          </security>          
         </binding>       
      </wsHttpBinding>    
    </bindings>



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

这是我的客户端app.config文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
  </configSections>
  <connectionStrings>
    <add name="WindowsFormsApplication.Properties.Settings.TestTestConnectionString" connectionString="Data Source=;Initial Catalog=TestTest;Persist Security Info=True;User ID=;Password=" providerName="" />
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
  </startup>
  <system.serviceModel>

    <client>
      <endpoint address="http://localhost/Service1.svc/wsHttp"
        binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1"
        contract="ServiceReference2.IService1" name="WSHttpBinding_IService1">
        <identity>
          <userPrincipalName value="Administrator" />
        </identity>
      </endpoint>
    </client>

    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_IService1" maxBufferPoolSize="2147483647"
          maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="64" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" />
        </binding>
      </wsHttpBinding>
    </bindings>
  </system.serviceModel>
</configuration>

我没看到什么?

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

一个问题是您从未将服务配置中定义的绑定分配给端点,因此服务将使用wsHttpBinding的默认值。尝试分配&#34; SafeServiceConf&#34;通过bindingConfiguration属性将配置绑定到您的端点:

<endpoint address="wsHttp" 
          binding="wsHttpBinding"
          bindingConfiguration="SafeServiceConf"
          contract="WcfService1.IService1" />

然后,服务将使用您指定的值而不是默认值。