沟通与REST服务

问题描述:

我使用下面的代码与REST服务进行通信的内部REST服务:沟通与REST服务

[ServiceContract()] 
interface ISomeService 
{ 
    [OperationContract()] 
    [WebGet()] 
    bool DoSomething(); 
} 

WebHttpBinding binding = new WebHttpBinding(); 
ChannelFactory<ISomeService> channelFactory = new ChannelFactory<ISomeService>(binding, "http://localhost:12000"); 
channelFactory.Endpoint.Behaviors.Add(new WebHttpBehavior()); 
ISomeService service = channelFactory.CreateChannel(); 
service.DoSomething(); 

它工作正常,在简单的测试应用程序,但在我的实际应用中,我想里面叫它我自己的REST服务:如果对我的REST服务进行了调用,我的服务应该调用另一个REST服务。

而且事情变得很奇怪。在这种情况下,上面的代码不起作用,因为如果将它放在服务方法中,它将发送POST请求而不是GET请求,这当然会导致“方法不允许”错误。我的代码中没有任何属性WebInvoke。

[ServiceContract()] 
class MainService 
{ 
    [OperationContract()] 
    [WebGet()] 
    public void Test() 
    { 
     CallDoSomething(); // code from above: Sends POST instead of GET request 
    } 
} 

岂是HTTP请求方法的变化?

+0

DORS'ISomeService'有'[WebGet]'其运作?在客户端。 – 2010-09-17 17:56:40

+0

是的,它有双方的[WebGet] :)我添加了一个演示到开始帖子。 – pdb 2010-09-17 18:15:58

很好,我在*上找到了答案。 :)

WCF Proxy Using Post Even Though WebGet Attribute is Specified (Only when called from another WCF service) - Causes 405 Error

using (new OperationContextScope(service as IContextChannel)) 
{ 
    service.DoSomething(); 
}