WCF Restful服务是否允许同样的方法公开为WebGet和WebInvoke?

WCF Restful服务是否允许同样的方法暴露为WebGet和WebInvoke,如方法重载? 这两种方法都可以从同一个URL访问。

对于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); 

是否可以使用WCF Restful Service?

是的,这是可能的,龙舌兰酒的答案非常接近预期:

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

但我不建议设计这样的api。 最好在enpoint描述中描述基础uri,UriTemplate应该反映资源标识符:

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

这是一个很好的建议:

REST最佳实践

RESTful API设计

是的,这是可能的,但有一些变化。 您所需要的只是更改方法的名称,但您可以为这两个端点使用相同的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方法访问。

或者我们可以使用别名来重载方法。 在这种情况下,我们可以使用这些别名(而不是原始名称)访问此操作:

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

在上面的示例中,可以使用别名在客户端站点访问这些方法; 这是AddInt和AddDouble。