2016-08-04 168 views
0

我使用WCF的basicHttpBinding協議。WCF遠程服務器返回意外的響應:(400)錯誤的請求

我收到大Class<T>(沒問題)。

但是我送大Class<T>我得到這個錯誤

拋出異常:在 'System.ServiceModel.ProtocolException' 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; 
} 

回答

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"..... 

您的服務應該能夠當客戶端發送他們接受更大的物體。

+0

感謝您的時間和幫助。你救了我1年。我kisssssss你 – Arman

相關問題