处理了ProtocolException,最大字符串内容长度配额为8192

时间:2012-06-07 14:17:13

标签: c# wcf

我有一个C#WCF服务和一个C#控制台应用程序,并希望将一个大文件从应用程序传递到WCF。当我传递字符串时,我得到了protocolexception,超出了最大字符串内容长度配额(8192),这是正确的。我将Apps.Config中的ReaderQuotas上的MaxStringContentLength更改为大量但没有任何反应,它仍然是错误。有谁知道为什么?我是否需要在web.config中添加内容。我的文件内容如下: -

Web.config

<?xml version="1.0"?>
<configuration>
<system.web>
    <compilation debug="true" targetFramework="4.0"/>
</system.web>
<system.serviceModel>
    <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>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>

App.Config中

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IService" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                useDefaultWebProxy="true">
                <readerQuotas maxDepth="32" maxStringContentLength="88192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <security mode="None">
                    <transport clientCredentialType="None" proxyCredentialType="None"
                        realm="" />
                    <message clientCredentialType="UserName" algorithmSuite="Default" />
                </security>
            </binding>
          <binding name="largeStrings" openTimeout="00:02:00" sendTimeout="00:02:00"
              maxBufferSize="524288" maxReceivedMessageSize="524288">
            <readerQuotas maxDepth="10" maxStringContentLength="524288" maxArrayLength="16384"
             maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:2745/ActuristDemo/Service.svc"
            binding="basicHttpBinding" bindingConfiguration="largeStrings"
            contract="ActuristWS.IService" name="largeStrings" />
    </client>
</system.serviceModel>
</configuration>

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

当您从客户端向服务发送大字符串时,您需要增加服务配置中的限制(即web.config)。例如:

<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="customHttpBinding" maxBufferSize="2147483647"
        maxReceivedMessageSize="2147483647" />
    <binding name="BasicHttpBinding_IMyWebService" closeTimeout="00:01:00"
        openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
        allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
        maxBufferSize="2147483647" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647"
        messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
        useDefaultWebProxy="true">
      <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="16384"
          maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <security mode="None">
        <transport clientCredentialType="None" proxyCredentialType="None"
            realm="" />
        <message clientCredentialType="UserName" algorithmSuite="Default" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<services>
  <service name="MyService.MyWcfService">
    <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMyWebService"
        contract="MyService.IMyWebService" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost/MyService/MyWcfService/" />
      </baseAddresses>
    </host>
  </service>
</services>