2010-09-28 369 views
1

我有以下WCF客戶端代碼:WCF REST客戶端異常

string add = String.Format("http://localhost:{0}/ServiceRequestRest",accessPort); 
var cf = new ChannelFactory<IServiceRequestRest>(new WebHttpBinding(), add); 
cf.Endpoint.Behaviors.Add(new WebHttpBehavior()); 
busService = cf.CreateChannel(); 

busService.DoMyStuff("hello",null); 

當我有「第一次機會異常中斷」激活代碼,最後一行拋出一系列異常。第一種例外情況是這樣的:

 
System.InvalidOperationException occurred 
    Message="Envelope Version 'EnvelopeNone (http://schemas.microsoft.com/ws/2005/05/envelope/none)' does not support adding Message Headers." 
    Source="System.ServiceModel" 
    StackTrace: 
     at System.ServiceModel.Channels.MessageHeaders.ValidateHeaderKind(HeaderKind headerKind) 

我不知道,我想「添加郵件頭」,但我選擇繼續執行,並且立即獲得下一個例外:

 
System.Net.WebException occurred 
    Message="The underlying connection was closed: The connection was closed unexpectedly." 
    Source="System" 
    StackTrace: 
     at System.Net.HttpWebRequest.GetResponse() 

與所有說,我很難理解問題是什麼。任何線索?

更新:我的REST呼叫的返回類型無法序列化,正如我從another site瞭解到的。這可能會導致這個問題,但在我修好後,我仍然有同樣的問題。當我使用常規catch塊來處理異常時,我所得到的只是「連接意外關閉」。還有什麼我可以錯過?

Update2:我想我找到了最後的問題。我得到了interference from Skype。代碼現在正在工作。 :)

回答

0

我看到的主要問題是您正在嘗試使用WebHttpBinding進行通信以使用WCF通道。

恕我直言,WebHttpBinding的全部目的是讓客戶端不必使用WCF堆棧與它進行通信。您只需使用任何HTTP堆棧來發出請求。