WCF远程服务器返回意外响应:(400)错误请求

时间:2016-08-04 13:32:26

标签: c# performance web-services wcf wcf-binding

我对WCF使用basicHttpBinding协议。

我收到大Class<T>(没问题)。

但我发送大Class<T>我收到此错误

  

抛出异常:&#39; System.ServiceModel.ProtocolException&#39;在   System.ServiceModel.Internals.dll

     

其他信息:远程服务器返回了意外情况   回复:(400)错误请求。

我的服务器配置是

<?xml version="1.0"?>
<configuration>
  <system.diagnostics>
    <sources>
      <source propagateActivity="true" 
              name="System.ServiceModel"
              switchValue="Warning,ActivityTracing">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener"
               name="Default">
            <filter type="" />
          </add>
          <add name="ServiceModelTraceListener">
            <filter type="" />
          </add>
        </listeners>
      </source>
      <source name="System.ServiceModel.MessageLogging"
              switchValue="Information, ActivityTracing">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener"
               name="Default">
            <filter type="" />
          </add>
          <add name="ServiceModelMessageLoggingListener">
            <filter type="" />
          </add>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add initializeData="d:\tourism\tourism\server\service\deltaservicewcf\web_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="d:\tourism\tourism\server\service\deltaservicewcf\web_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="false">
      <messageLogging logEntireMessage="true" logMalformedMessages="true"
        logMessagesAtServiceLevel="false" logMessagesAtTransportLevel="true" />
    </diagnostics>
    <bindings>
      <basicHttpBinding>
        <binding name="NewBinding0" closeTimeout="01:01:00" openTimeout="01:01:00"
          receiveTimeout="01:10:00" sendTimeout="01:01:00" maxBufferPoolSize="2147483647"
          maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
          transferMode="Streamed">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
            maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
        </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="myHttpBindingBihaviour" name="DeltaServiceWCF.DeltaService1">
        <endpoint behaviorConfiguration="myHttpBindingEndPointBihaviour"
          binding="basicHttpBinding" bindingConfiguration="" name="" contract="DeltaServiceWCF.IDeltaService1"
          isSystemEndpoint="false" />
        <host>
          <timeouts closeTimeout="01:00:10" openTimeout="01:01:00" />
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="myHttpBindingBihaviour">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceTimeouts transactionTimeout="01:00:01" />
          <dataContractSerializer ignoreExtensionDataObject="true"  maxItemsInObjectGraph="2147483647"/>
          <serviceThrottling maxConcurrentCalls="2000" maxConcurrentSessions="1000"  maxConcurrentInstances="1000" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="myHttpBindingEndPointBihaviour" >
          <dataContractSerializer ignoreExtensionDataObject="true" maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <!--<protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>-->
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint name="" maxBufferSize="2147483647" maxBufferPoolSize="4194304"
          maxReceivedMessageSize="2147483647" helpEnabled="true" automaticFormatSelectionEnabled="true"
          crossDomainScriptAccessEnabled="true">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
            maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
        </standardEndpoint>
      </webHttpEndpoint>
    </standardEndpoints>

  </system.serviceModel>
  <system.web>
    <httpRuntime maxRequestLength="2147483647" maxQueryStringLength="2097151" maxUrlLength="2097151" maxWaitChangeNotification="2147483647" requestValidationMode="4.0" executionTimeout="110000" targetFramework="4.5"/>
    <compilation debug="true"/>
    <customErrors mode="Off"/>
  </system.web>
  <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>

  <!--<system.web.extensions>
    <scripting>
      <webServices>
        <authenticationService enabled="true"
            requireSSL="false"  />
      </webServices>
    </scripting>
  </system.web.extensions>-->
</configuration>

我的客户端代码是

public static EndpointAddress EndPoint = new EndpointAddress("http://192.168.1.102:9159/DeltaService1.svc");

public static BasicHttpBinding CreateBasicHttp()
{

    var binding = new BasicHttpBinding();
    binding.MaxReceivedMessageSize = 2147483647;
    binding.MessageEncoding = WSMessageEncoding.Text;
    binding.ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas()
    {
        MaxArrayLength = 2147483647,
        MaxStringContentLength = 2147483647,
        MaxBytesPerRead = 2147483647,
        MaxDepth = 2147483647,
        MaxNameTableCharCount = 2147483647,
    };
    binding.MaxBufferPoolSize = 2147483647;
    binding.MaxBufferSize = 2147483647;
    binding.TransferMode = TransferMode.Streamed;
    binding.Security = new BasicHttpSecurity();

    binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;

    binding.Security.Mode = BasicHttpSecurityMode.None;
    binding.Security.Transport = new HttpTransportSecurity();
    binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
    binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
    binding.Security.Transport.Realm = "";

    var timeout = new TimeSpan(1, 1, 0);
    binding.SendTimeout = timeout;

    binding.CloseTimeout = timeout;
    binding.OpenTimeout = timeout;
    binding.ReceiveTimeout = timeout;
    binding.SendTimeout = timeout;
    binding.TextEncoding = Encoding.UTF8;

    ChannelFactory<IDeltaService1> factory = new ChannelFactory<IDeltaService1>(binding, EndPoint);

    foreach (OperationDescription op in factory.Endpoint.Contract.Operations)
    {
        var dataContractBehavior = op.Behaviors.Find<DataContractSerializerOperationBehavior>();
        if (dataContractBehavior != null)
        {
            dataContractBehavior.IgnoreExtensionDataObject = true;
            dataContractBehavior.MaxItemsInObjectGraph = int.MaxValue;
        }
    }

    IDeltaService1 channel = factory.CreateChannel();

    return binding;
}

1 个答案:

答案 0 :(得分:0)

您无法向服务发送大型对象的原因是您从未将定义的basicHttpBinding分配给服务端点。

在您的服务配置中,您有:

<bindings>
  <basicHttpBinding>
    <binding name="NewBinding0".....

但是当您定义端点时,不要为其指定“NewBinding0”:

<endpoint behaviorConfiguration="myHttpBindingEndPointBihaviour"
      binding="basicHttpBinding" bindingConfiguration=""....

由于bindingConfiguration元素未指定绑定配置,因此使用basicHttpBinding的默认值,这就是您无法发送大型对象的原因。

通过bindingConfiguration属性将“NewBinding0”分配给您的终端,如下所示:

<endpoint behaviorConfiguration="myHttpBindingEndPointBihaviour"
      binding="basicHttpBinding" bindingConfiguration="NewBinding0".....

当客户端发送它们时,您的服务应该能够接收更大的对象。