简体   繁体   English

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

[英]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: 这是一个很好的建议:

REST Best practices REST最佳实践

RESTful API Design RESTful API设计

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.

相关问题 WCF服务:带有AD的WebGet和WebInvoke的限制-具有匿名访问权限的网站的安全角色 - WCF service: restrictions for WebGet and WebInvoke with AD - security roles for a website with anonymous access WebGet在功能上等同于WebInvoke(Method =“GET”)吗? - Is WebGet functionally equivalent to WebInvoke(Method = “GET”)? 为什么WebInvoke不允许使用此方法,但WebGet是否可以? - Why is this method not allowed for WebInvoke, but OK for WebGet? 当您使用REST的webinvoke或webget时,可以忽略WCF [operationcontract]? - WCF [operationcontract] can be ignored when you use REST's webinvoke or webget? WCF rest api-[WebGet]工作,但未找到[WebInvoke(…GET…)]的端点 - WCF rest api - [WebGet] working, but endpoint not found for [WebInvoke(…GET…)] 在WCF上为REST服务实现WebInvoke Method =“ POST”时出现问题 - Problem Implementing WebInvoke Method=“POST” for a REST Service on WCF 通过WCF数据服务中的WebGet方法检索导航属性 - Retrieving Navigation Properties through WebGet method in WCF Data Service 如何从Angular js脚本调用WCF服务WebGet方法 - how to call WCF service WebGet method from Angular js script WCF不解码WebGet方法上的参数 - WCF not decoding parameters on WebGet method WCF并使用WEBGET在浏览器中测试服务 - WCF and using WEBGET to test service in browser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM