Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF Restful service GET/POST

Tags:

rest

c#

wcf

Can I do something like this?

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

Idea here being that I can pass first parameter( integer ) in the url, but secondParam comes from POST. Is this even possible?

I started with WCF REST and not sure about how parameters are assigned. Any pointers will be helpful thank you

like image 680
ASR Avatar asked Jul 30 '12 17:07

ASR


People also ask

Is it possible to use RESTful services using WCF?

You can use WCF to build RESTful services in . NET. REST (Representational State Transfer) is an architecture paradigm that conforms to the REST architecture principles. The REST architecture is based on the concept of resources: It uses resources to represent the state and functionality of an application.

What is WebInvoke method in WCF?

The WebInvoke attribute exposes services using other HTTP verbs such as POST, PUT, and DELETE. The default is to use POST, but it can be changed by setting the Method property of the attribute. These operations are meant to modify resources; therefore, the WebInvoke attribute is used to make modifications to resources.


1 Answers

Yes you can, here is from 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) {...}
    ...
}

As for me, this kind of designing RESTful web services is terrible. This ServiceContrcat is:

  • unmaintainable, brittle remote interface
  • Have to create too many methods
  • Polymorphism is absent

I believe, that remote interface should be stable and flexible, we can use message based approach for designing web services.

You can find detailed explanation here: Building RESTful Message Based Web Services with WCF, code samples here: Nelibur and Nelibur nuget package here

like image 99
GSerjo Avatar answered Oct 08 '22 15:10

GSerjo