2010-09-28 172 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堆栈来发出请求。