Question

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?

Was it helpful?

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

OTHER TIPS

Yes it is possible but with a little changes. All you need is to change the name of the methods but you can use the same url for both of these endpoints. For example you can do something like this:

[OperationContract]
[WebGet(UriTemplate = "GetData/{value}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Xml)]
        string GetData2(string value);

[OperationContract]
[WebInvoke(UriTemplate = "GetData/{value}", RequestFormat=WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Xml)]
        string GetData(string value);

First will be accessible only by GET request and second only by POST method.

Or we can use aliases for overloaded methods. In this case we can access to this operation using these aliases (not by original name):

[ServiceContract]
interface IMyCalculator
{
    //Providing alias AddInt, to avoid naming conflict at Service Reference
    [OperationContract(Name = "AddInt")]
    public int Add(int numOne, int numTwo);

    //Providing alias AddDobule, to avoid naming conflict at Service Reference
    [OperationContract(Name = "AddDouble")]
    public double Add(int numOne, double numTwo); 
}

In above example, these methods can be accessed at client site by using their alias; which are AddInt and AddDouble.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top