[英]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? WCF Restful服务是否允许同样的方法暴露为WebGet和WebInvoke之类的方法重载? Both method shoud be accessible from same URL.
这两种方法都可以从同一个URL访问。
For Ex. 对于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? 是否可以使用WCF Restful Service?
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. 但我不建议设计这样的api。 It's better to describe base uri in enpoint description, UriTemplate should reflect resource identifier:
最好在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);
}
Here is good advice: 这是一个很好的建议:
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.
您只需要更改方法的名称,但可以为这两个端点使用相同的URL。 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. 第一个只能通过GET请求访问,第二个只能通过POST方法访问。
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.
这是AddInt和AddDouble。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.