我已经编写了一个Windows,它通过C#提供REST api。此服务需要调用另一个web服务,该服务也使用REST api。我的服务可以与其他服务完美地通信,除非有人调用了我的服务,并且它当前正在响应。我写了一个简单的测试:
public void TestConnection()
{
WebChannelFactory<IOtherService> wf = new WebChannelFactory<IOtherService>(otherURI);
IOtherService service = wf.CreateChannel();
service.Test();
}
[ServiceContract]
public interface IOtherService
{
[WebGet(UriTemplate = "services/account?username=testuser&password=d625a4de623dce36af2b75102eaf0ce7&locale=en-US", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
[OperationContract]
AccountServiceInfo Test();
}通常,当我调用TestConnection时,它工作得很好,但是当有人调用我的服务,要求我调用TestConnection时,另一个服务看到的是POST而不是GET,并返回400。有没有人知道为什么会出现这种情况,或者我如何解决它?谢谢。
发布于 2012-06-08 13:56:25
当在已经有OperationContext的WCF服务中使用WebChannelFactory时,您可能需要先创建一个新的上下文,然后才能使用WebChannelFactory创建的通道成功调出。
public void TestConnection()
{
var wf = new WebChannelFactory<IOtherService>(otherURI);
var service = wf.CreateChannel();
using ((IDisposable)service)
using (new OperationContextScope((IContextChannel)service))
{
service.Test();
}
}http://blogs.msdn.com/b/pedram/archive/2008/07/19/webchannelfactory-inside-a-wcf-service.aspx
https://stackoverflow.com/questions/10940594
复制相似问题