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

Can the WCF Restful service allow the same method to be called as overloading the WebGet and WebInvoke methods? Both methods can be accessed from the same URL.

For example

 [ServiceContract]
public interface IWeChatBOService
{

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

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

Can I use the WCF Restful Service?

+4
source share
3 answers

Yes, it is possible and Tequila's answer is very close to what was 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 designing such an api. It is better to describe the base uri in the enpoint description, the UriTemplate should reflect the 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 a good tip:

Best REST Techniques

RESTful API Design

+2
source

, , . , , , URL . , - :

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

GET POST.

0

Or we can use aliases for overloaded methods. In this case, we can access this operation using these aliases (not by the 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 the above example, these methods can be obtained on the client’s site using their alias; which are AddInt and AddDouble.

0
source

Source: https://habr.com/ru/post/1537084/


All Articles