WCF使用大消息调用服务

时间:2012-01-06 15:25:26

标签: wcf

我正在调用WCF服务的方法。该方法将图像中的图形传递给服务。

所有电话都会抛出异常说:

The remote server returned an unexpected response: (400) Bad Request

我已经阅读了很多有关此问题的帖子,其中大多数都建议增加maxReceivedMessageSize。我试图修改此设置和其他设置,但它没有解决问题。

最后,我在服务器上激活了跟踪,说:

The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element.

我的配置文件中的值设置为65536000.

我的配置文件:

<?xml version="1.0"?>
<configuration>
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel.MessageLogging" switchValue="Verbose, ActivityTracing">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type="" />
          </add>
          <add name="ServiceModelMessageLoggingListener">
            <filter type="" />
          </add>
        </listeners>
      </source>
      <source name="System.ServiceModel" switchValue="Verbose,ActivityTracing"
        propagateActivity="false">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type="" />
          </add>
          <add name="ServiceModelTraceListener">
            <filter type="" />
          </add>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add initializeData="C:\Trace\messages.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
        name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp">
        <filter type="" />
      </add>
      <add initializeData="C:\Trace\tracelog.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
        name="ServiceModelTraceListener" traceOutputOptions="Timestamp">
        <filter type="" />
      </add>
    </sharedListeners>
    <trace autoflush="true" />
  </system.diagnostics>
  <system.serviceModel>
    <diagnostics wmiProviderEnabled="true">
      <messageLogging logEntireMessage="true" logMalformedMessages="true"
        logMessagesAtTransportLevel="true" />
      <endToEndTracing propagateActivity="true" activityTracing="true"
        messageFlowTracing="true" />
    </diagnostics>
    <behaviors>
      <endpointBehaviors>
        <behavior name="Beh">
          <dataContractSerializer maxItemsInObjectGraph="10000000"/>
          <enableWebScript />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <webHttpBinding>
        <binding name="WebBinding" closeTimeout="00:10:00" openTimeout="00:10:00"
          sendTimeout="00:10:00" maxBufferSize="65536000" maxBufferPoolSize="524288000"
          maxReceivedMessageSize="65536000">
          <readerQuotas maxDepth="500000000" maxStringContentLength="500000000"
            maxArrayLength="500000000" maxBytesPerRead="500000000" maxNameTableCharCount="500000000" />
          <security mode="Transport" />
        </binding>
      </webHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="ServiceBehavior" name="WcfService1.PleasureKraftService">
        <clear />
        <endpoint behaviorConfiguration="Beh" binding="webHttpBinding"
          bindingConfiguration="WebBinding" name="Basic" contract="WcfService1.IMyService"
          listenUriMode="Explicit">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpsBinding" contract="WcfService1.IMyService" />
      </service>
    </services>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.web>
    <compilation debug="false"/>
<customErrors mode="Off"/>
  </system.web>
  <connectionStrings>
    <add name="Ent" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=xxxxxx;Initial Catalog=Eu;User ID=xxxxx;Password=xxxxx;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient"/>
  </connectionStrings>
</configuration>

2 个答案:

答案 0 :(得分:2)

您可能需要在客户端和服务上定义MaxReceivedMessageSize属性。

答案 1 :(得分:0)

  1. 增加消息最大配额,可能超时也取决于消息大小 - 您可以以声明方式或以编程方式执行此操作,如下所示。

    BasicHttpBinding Binding = new BasicHttpBinding();

    Binding.ReceiveTimeout = new TimeSpan(???, 0, 0);
    Binding.SendTimeout = new TimeSpan(???, 0, 0);
    Binding.OpenTimeout = new TimeSpan(???, 0, 0);
    Binding.CloseTimeout = new TimeSpan(???, 0, 0);
    
    Binding.ReaderQuotas.MaxStringContentLength = ???;
    Binding.ReaderQuotas.MaxArrayLength = ???;
    Binding.ReaderQuotas.MaxBytesPerRead = ???;
    Binding.ReaderQuotas.MaxNameTableCharCount = ???;
    Binding.MaxReceivedMessageSize = ???;
    Binding.MaxBufferPoolSize = ???;
    
  2. 将您的有效负载拆分为较小的块

  3. 选项1:自己填充有效负载

    选项2:频道分块     http://msdn.microsoft.com/en-us/library/aa717050(v=vs.110).aspx