WebChannelFactory inside a WCF Service

When using a WebChannelFactory inside a WCF service that already has an OperationContext, you may need to create a new context before being able to successfully callout using a channel created by the WebChannelFactory. (Notice the line in bold)

public class RelationService : IRelationService


  public Relation[] GetRelations()


    var factory = new WebChannelFactory<ICustomerService>(

      new Uri("http://localhost/customerservice/customers.svc"));

    var proxy = factory.CreateChannel();

    using ((IDisposable)proxy)

    using (new OperationContextScope((IContextChannel)proxy))


      var customers = proxy.GetCustomers();

      return customers.Select(c => new Relation(c.Name, c.Age)).ToArray();




In the above example, GetRelations() is a RESTful service operation calling into another RESTful service located at the Uri shown above.

Without using the new context, you may get an exception similar to the following:

ProtocolException: The remote server returned an unexpected response: (405) Method Not Allowed.

When investigating further, you may notice that WCF could be using an incorrect HTTP verb for communicating with the service that exposes the GetCustomers() operation.

As it happens, here is how ICustomerService service contract looks like:


public interface ICustomerService




      UriTemplate = "/customers",

      ResponseFormat = WebMessageFormat.Xml,

      BodyStyle = WebMessageBodyStyle.Bare


  List<Customer> GetCustomers();


As you can see it is expecting the GET verb. Without creating a new context, WCF ends up using the POST verb which will eventually cause the above exception.

Comments (7)
  1. Alex says:

    Many thanks for this hint!

    I was puzzled with this issue for few days.

    Would also like to know why without new context WCF uses POST instead of GET?

  2. FooBar says:

    WOWWW… Thanks for documenting it.. it wasted my complete day.. this seem like a nasty bug..

  3. AB says:

    Thankfully found this before spending too much time scratching my head!

    Is there any reason you'd ever want to reuse the current scope while calling another service?

  4. Vijay says:

    Good one

    I spend one whole day.

    why it was happend?

  5. rokshill says:

    Thanks a lot.

Comments are closed.

Skip to main content