Search code examples
c#.netwcfwcf-rest

Can WCF Restful service allow same method expose as WebGet and WebInvoke?


Can WCF Restful service allow same method expose as WebGet and WebInvoke like method overloading? Both method shoud be accessible from same URL.

For Ex.

 [ServiceContract]
public interface IWeChatBOService
{

    [WebGet(UriTemplate = "WeChatService/{username}")]
    [OperationContract]
    string ProcessRequest(string MsgBody);

    [WebInvoke(Method = "POST", UriTemplate = "WeChatService/{username}")]
    [OperationContract]
    string ProcessRequest(string MsgBody);

Is it possible to do with WCF Restful Service?


Solution

  • Yes, that's possible and Tequila's answer is very close to what is expected:

    [ServiceContract]
    public interface IWeChatBOService
    {
    
        [WebGet(UriTemplate = "WeChatService/{msgBody}")]
        [OperationContract]
        string ProcessRequest(string msgBody);
    
        [WebInvoke(Method = "POST", UriTemplate = "WeChatService")]
        [OperationContract]
        string ProcessRequest2(string msgBody);
    }
    

    But I would not recommend to design such api. It's better to describe base uri in enpoint description, UriTemplate should reflect resource identifier:

    [ServiceContract]
    public interface IWeChatBOService
    {
    
        [WebGet(UriTemplate = "messages/{messageId}")]
        [OperationContract]
        string GetMessage(string messageId);
    
        [WebInvoke(Method = "POST", UriTemplate = "messages")]
        [OperationContract]
        string InsertMessage(string message);
    }
    

    Here is good advice:

    REST Best practices

    RESTful API Design