views:

63

answers:

1

Hi,

i use the following code to communicate with a REST service:

[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();

It works fine in simple test applications, but in my real application i want to call it inside my own REST service: If a call to my REST service is made, my service should make a call to another REST service.

And there things go weird. In this situation the code above does not work because if it is placed inside a service method it sends a POST request instead of a GET request, which of course results in a "Method not allowed" error. I don't have the attribute WebInvoke anywhere in my code.

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

How can it be that the HTTP request method changes?

A: 

Ah great I found the answer on stackoverflow. :)

http://stackoverflow.com/questions/3294766/wcf-proxy-using-post-even-though-webget-attribute-is-specified-only-when-called

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