如果第一个服务响应对它的调用,则调用另一个Web服务的WebService具有不同的行为

时间:2012-06-07 22:12:22

标签: c# wcf

我编写了一个C#Windows服务,通过WCF提供REST API。此服务需要调用另一个也使用REST API的Web服务。我的服务可以与其他服务完美通信,除非有人拨打我的服务并且当前正在响应。我写了一个简单的测试:

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.有没有人知道为什么这可能是案件或我如何解决它?感谢。

1 个答案:

答案 0 :(得分:1)

在已经有WebChannelFactory的WCF服务中使用OperationContext时,您可能需要创建新的上下文才能使用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

相关问题