wcfresturitemplatewebchannelfactory

Accessing WCF REST operation using WebChannelFactory when the operation has UriTemplate specified


I have a WCF Rest service as per the code snippet below

[ServiceContract]
public interface IService
{
     [OperationContract]
        [WebInvoke(UriTemplate="/SaveList/Foos/",Method="POST")]
        bool SaveList(List<Foo> myFoos);

}

The service is a self hosted service and exposes a an endpoint with webHttpBinding. I have a client console application which has the following code snippet:

public void Send()
{
   var myObj = new Foo{Id=1, Name="Test"};
   using (WebChannelFactory<OurService.Services.IOurService> cf = new WebChannelFactory<IOurService>("WebHttpBinding_IService"))
   {
       IUtranetService channel = cf.CreateChannel();
       using (new OperationContextScope(channel as IContextChannel))
       {
           var status = channel.SaveList(new[] { myObj });

        }

    }

} 

The client code throws an exception as follows: There was no endpoint listening at "http://localhost:1133/Service/SaveList" that could accept the message.

I am not sure where I am going wrong. If I remove the UriTemplate from the service contract, the client returns back the right response.

Any help would be greatly appreciated.

Regards Ruchitra


Solution

  • Your URI template is incorrect. You need to change it to this:

     [WebInvoke(UriTemplate="/SaveList/Foos/SaveList",Method="POST")] 
    

    It does not automatically append your method name.

    Also FYI as per the comment I left, you dont need Method="Post"