2011-11-10 78 views
1

我已經寫有下列配置的WCF服務:HTTP錯誤請求(400)錯誤與WCF服務與多個端點

<system.serviceModel> 
    <services> 
    <service name="SyncService" behaviorConfiguration="SyncServiceBehavior"> 
     <endpoint name="DataBinding" address="/DataBinding" binding="wsHttpBinding" contract="ISyncService"> 
       <identity> 
       <dns value="localhost"/> 
       </identity> 
     </endpoint> 
     <endpoint name="FileBinding" address="/FileBinding" binding="basicHttpBinding" bindingConfiguration="httpLargeMessageStream" contract="ISyncService"> 
       <identity> 
       <dns value="localhost"/> 
       </identity> 
      </endpoint> 
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
      </service> 
     </services> 
     <bindings> 
      <basicHttpBinding> 
      <binding name="httpLargeMessageStream" maxReceivedMessageSize="2147483647" transferMode="Streamed" messageEncoding="Mtom" /> 
      </basicHttpBinding> 
     </bindings> 
     <behaviors> 
      <serviceBehaviors> 
      <behavior name="SyncServiceBehavior"> 
       <serviceMetadata httpGetEnabled="true"/> 
       <serviceDebug includeExceptionDetailInFaults="true"/> 
       <dataContractSerializer maxItemsInObjectGraph="2147483647"/> 
      </behavior> 
      </serviceBehaviors> 
     </behaviors> 
     </system.serviceModel> 

在客戶端,我建立了代理來動態消費服務:

private static RemoteFileProviderProxy CreateDynamicFileProxy(string endPointAddress) 
     { 
      //endPointAddress = http://localhost/SyncService.svc/FileBinding 
      BasicHttpBinding binding = new BasicHttpBinding(); 

      binding.Name = "httpLargeMessageStream"; 
      binding.TransferMode = TransferMode.Streamed; 
      binding.MessageEncoding = WSMessageEncoding.Mtom; 
      binding.MaxReceivedMessageSize = int.MaxValue; 

      ServiceEndpoint endPoint = new ServiceEndpoint(ContractDescription.GetContract (typeof(ISyncService)), binding, new EndpointAddress(endPointAddress)); 

      endPoint.Name = "FileBinding"; 

      ChannelFactory<ISyncService> channelFactory = new ChannelFactory<ISyncService>(endPoint); 

      return new RemoteFileProviderProxy((ISyncService)channelFactory.CreateChannel()); 
     } 

起初,我只有數據綁定端點,駐留在默認的地址(而不是它目前確實的數據綁定地址),我用同樣的客戶端代碼來使用該服務端點和一切運行正常。當我添加FileBinding端點並將DataBinding端點移動到其新位置http://localhost/SyncService.svc/DataBinding時,我不再能夠連接到任一端點,而是在首次使用時收到「錯誤請求(400)」。

綜觀服務跟蹤查看器,我看到被拋出關於System.Xml.XmlException以下附加信息:

有與從網絡接收的XML存在問題。有關更多詳情,請參閱內部例外

內部異常:

消息的主體不能被讀取,因爲它是空的。

堆棧跟蹤:

at System.ServiceModel.Channels.HttpRequestContext.CreateMessage() 
at System.ServiceModel.Channels.HttpChannelListener.HttpContextReceived(HttpRequestContext context, Action callback) 
at System.ServiceModel.Activation.HostedHttpTransportManager.HttpContextReceived(HostedHttpRequestAsyncResult result) 
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.HandleRequest() 
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.BeginRequest() 
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequest(Object state) 
at System.Runtime.IOThreadScheduler.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped) 
at System.Runtime.Fx.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nativeOverlapped) 
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP) 

當我在瀏覽器中查看SyncService.svc,我得到了「你已經創建了一個服務」頁面,一切看起來是正確的。 wsdl也看起來合適。當我去http://localhost/SyncService.svc/FileBinding時,會加載一個空白頁面(與我去http://localhost/SyncService.svc/XXX之類的時候出現的錯誤頁面相反)。

我經歷了一些類似的問題涉水,都無濟於事,包括:

HTTP Bad Request error when requesting a WCF service contract

Large WCF web service request failing with (400) HTTP Bad Request

人對如何解決這個任何想法或建議?

+0

有趣的更新,即使在引發錯誤請求時正在使用FileBinding的代理對象,服務跟蹤查看器將顯示在DataBinding端點處偵聽的錯誤。通過翻轉命令,端點出現在服務器上的配置文件中,我可以在FileBinding端點收聽時引發相同的錯誤。 –

+0

確實刪除配置中的地址屬性的斜槓有什麼作用? –

+0

@degorolls,no。我已經嘗試了兩種方法,並得到完全相同的結果。 –

回答

0

您可以嘗試檢查使用Fiddler等工具發送到服務器的請求嗎?肥皂信封中一定有東西會導致問題。

也嘗試初始發送小數據以確保您的服務可訪問,然後嘗試更大的數據,以便您可以縮小問題的位置。

您是否實現了異步模式,或者您的Web服務是否標記爲Async = True。

如果您也可以發佈您的服務,這將有所幫助。

1

事實證明,這個問題不是由契約對象引起的,而是由一個它未被引用的對象引起的。在我的情況下,我忘了在服務器上安裝所有必需的Microsoft Sync Framework作品。

我想這個故事的寓意是這個錯誤是相當具有誤導性的,並且也適用於所有子對象!