【问题标题】:WCF Restful service GET/POSTWCF Restful 服务 GET/POST
【发布时间】:2012-07-30 17:06:03
【问题描述】:

我可以这样做吗?

[OperationContract]    
[WebInvoke
  (  
    Method = "POST",
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.Bare,
    UriTemplate = "/abc{integerParam}"
  )
]
ResultStruct abc( int integerParam, CustomClass secondParam );

这里的想法是我可以在 url 中传递第一个参数(整数),但 secondParam 来自 POST。这甚至可能吗?

我从 WCF REST 开始,不确定如何分配参数。任何指示都会有所帮助,谢谢

【问题讨论】:

  • 是否有必须使用 WCF 的要求?
  • 是的,必须使用 WCF。 @MIke 这个例子并不是我想要做的,它只使用查询字符串。我正在尝试使用查询字符串发布?
  • 好的,看看这个。他们有一个发布的正文并接收查询参数。 stackoverflow.com/questions/11261119/wcf-post-with-query-string
  • Thnx 这就是我起初的想法,要么访问查询字符串,要么从代码中发布,但想要第二个意见。 Tx 为您提供帮助。

标签: c# wcf rest


【解决方案1】:

是的,你可以,这里来自A Guide to Designing and Building RESTful Web Services

[ServiceContract]
public partial class BookmarkService
{
    [WebInvoke(Method = "PUT", UriTemplate = "users/{username}")]
    [OperationContract]
    void PutUserAccount(string username, User user) {...}

    [WebInvoke(Method = "DELETE", UriTemplate = "users/{username}")]
    [OperationContract]
    void DeleteUserAccount(string username) {...}

    [WebInvoke(Method = "POST", UriTemplate = "users/{username}/bookmarks")]
    [OperationContract]
    void PostBookmark(string username, Bookmark newValue) {...}

    [WebInvoke(Method = "PUT", UriTemplate = "users/{username}/bookmarks/{id")]
    [OperationContract]
    void PutBookmark(string username, string id, Bookmark bm) {...}

    [WebInvoke(Method = "DELETE", UriTemplate = "users/{username}/bookmarks/{id}")]
    [OperationContract]
    void DeleteBookmark(string username, string id) {...}
    ...
}

对我来说,这种设计 RESTful Web 服务的方式很糟糕。这个 ServiceContrcat 是:

  • 不可维护,脆弱的远程界面
  • 必须创建太多方法
  • 没有多态性

我相信,远程接口应该稳定灵活,我们可以使用基于消息的方法来设计网络服务。

你可以在这里找到详细的解释:Building RESTful Message Based Web Services with WCF,代码示例在这里:Nelibur和Nelibur nuget包here

【讨论】:

  • 是的,我要删除这篇文章;值得指出一些事情。我阅读了此答案中链接的 CodeProject 文章,您正在通过创建超通用、灵活的接口在代码中做一些非常酷的事情。但是,您并没有创建 RESTful 接口。有特定的原则围绕什么使 API 成为 RESTful 和 IJsonService 毫无疑问不是 RESTful 服务定义。没有什么说你不能继续使用这种 API 创建风格,但你不能称之为 RESTFul。非常酷的代码......
  • 谢谢 :) 对我来说,这是一个范例,而不是一个实现。轮子是汽车吗?不,但如果有其他轮子等,它就是一辆车……IJsonService 也是如此。宁静的服务它是一种范式,服务应该如何表现而不是实现。 en.wikipedia.org/wiki/Representational_state_transfer
猜你喜欢
  • 2011-09-30
  • 1970-01-01
  • 2014-03-01
  • 2011-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多