Saturday, February 21, 2009 8:07 PM kevinisom

REST as Simple as Possible

I've been doing a lot of REST based services lately and for something that is as simple a concept as REST it is actually not the easiest thing to find guidance for a .Net developer. It's simple, or at least it should be. My first stop was the WCF Rest Starter Kit. I thought great, surely this would get me going with no fuss. I only wish it were so. I thought I could create a project with it and just get going. Instead what I got was a whole heap of code that didn't really start me on the way at all. I won't totally bag on the starter kit. I rather like the items around the atom publishing protocol. I briefly considered just writing an http handler for that but I knew that there had to be a better way.

Enter WCF.

It is actually incredibly easy to write REST services in .net. with WCF. 

Step 1

Create a new WCF service  in your website.

Step 2

Go to the cruft VS adds to your Web.config when creating your service and remove it and replace it with the following

   1:  <system.serviceModel>
   2:      <behaviors>
   3:          <endpointBehaviors>
   4:              <behavior name="web">
   5:                  <webHttp/>
   6:              </behavior>
   7:          </endpointBehaviors>
   8:      </behaviors>
   9:      <services>
  10:          <service name="Service">
  11:              <endpoint address=""
  12:                    binding="webHttpBinding"
  13:                    contract="IService"
  14:                    behaviorConfiguration="web"/>
  15:      </service>
  16:      <service name="WebsiteService">
  17:          <endpoint address=""
  18:                   binding="webHttpBinding"
  19:                   contract="WebsiteService"
  20:                   behaviorConfiguration="web"/>
  21:          </service>
  22:      </services>
  23:  </system.serviceModel>


And all you have to do it is add some attributes to your service contract

   1:  using System.ServiceModel;
   2:  using System.ServiceModel.Web;
   3:   
   4:  [ServiceContract]
   5:  public interface IService
   6:  {
   7:      [OperationContract]
   8:      [WebGet(UriTemplate = "/SayHello", ResponseFormat = WebMessageFormat.Xml)]
   9:      string SayHello();
  10:   
  11:      [OperationContract]
  12:      [WebGet(UriTemplate = "/SayHello/{user}", ResponseFormat = WebMessageFormat.Xml)]
  13:      string SayHelloToUser(string user);
  14:   
  15:      [OperationContract]
  16:      [WebGet(UriTemplate = "/SayHelloInJson/{user}", ResponseFormat = WebMessageFormat.Json)]
  17:      string SayHelloToUserInJson(string user);
  18:   
  19:      [OperationContract]
  20:      [WebGet(UriTemplate = "/GetPackage", ResponseFormat = WebMessageFormat.Json)]
  21:      Package GetPackage();
  22:   
  23:   
  24:      [OperationContract]
  25:      [WebInvoke(Method = "POST", UriTemplate = "/PlainPost", ResponseFormat = WebMessageFormat.Xml,
  26:          BodyStyle = WebMessageBodyStyle.Wrapped)]
  27:      int PlainPost(string username, string name, string message);
  28:   
  29:  }

Then implement the interface and you are good to go. I'll cover consuming these with both .Net and Javascript in a later post.

The attributes are where the real magic happens

The WebGet attribute tells WCF that this method is invoked via an HTTP Get Request.

The UriTemplate attribute property sets the URI of the call so on SayHello the url requested would be ~/Service.svc/SayHello

The ResponseFormat attribute property tells the service to return either XML or Json depending on the value of the WebMessageFormat enum

The WebInvoke attribute gives you to specify the type of HTTP request you make. The default method is POST.

The BodyStyle attribute property sets the body style of the messages sent to and from the service.

There you have it. Simple REST from WCF.

Cheers

Filed under: , ,

Comments

# Bruno Campagnolo de Paula weblog &raquo; Resumo do dia para 2009-02-21

Pingback from  Bruno Campagnolo de Paula weblog &raquo; Resumo do dia para 2009-02-21

# REST as Simple as Possible - Kevin Isom

Sunday, February 22, 2009 4:01 PM by DotNetShoutout

Thank you for submitting this cool story - Trackback from DotNetShoutout

# Bruno Campagnolo de Paula weblog &raquo; Resumo do dia para 2009-02-22

Pingback from  Bruno Campagnolo de Paula weblog &raquo; Resumo do dia para 2009-02-22

# Bruno Campagnolo de Paula weblog &raquo; Resumo do dia para 2009-03-22

Pingback from  Bruno Campagnolo de Paula weblog &raquo; Resumo do dia para 2009-03-22

# re: REST as Simple as Possible

Saturday, March 05, 2011 2:46 PM by Jane Meetington

It is rather interesting for me to read that post. Thanks for it. I like such themes and everything that is connected to them. I would like to read a bit more soon.      

Jane  Meetington    

<a href="www.jammer-store.com/">jam cell phone signals</a>

# re: REST as Simple as Possible

Friday, March 18, 2011 4:49 PM by Sara Kripke

It was certainly interesting for me to read the blog. Thanx for it. I like such themes and anything connected to them. I would like to read more soon.        

Sara  Kripke      

<a href="rome-escort.info/">escort a roma</a>

# re: REST as Simple as Possible

Monday, March 28, 2011 12:39 PM by Brandy Stone

It is extremely interesting for me to read the post. Thanks for it. I like such themes and everything that is connected to this matter. I would like to read a bit more soon.        

Brandy  Stone        

<a href="www.phone-blocker.com/">mobile frequency jammer</a>

# re: REST as Simple as Possible

Tuesday, April 12, 2011 3:50 PM by Jenny Swift

It is rather interesting for me to read that post. Thank author for it. I like such themes and anything connected to them. I definitely want to read more on this site soon.          

Jenny  Swift          

<a href="milanescorts.com/">agenzia escort accompagnatore milano</a>

# re: REST as Simple as Possible

Wednesday, May 30, 2012 9:29 AM by pletcherwnk

. a producendo ogni singolo bambino "figura l'applicazione out "  www.louisvuittonborse2012.net

# re: REST as Simple as Possible

Wednesday, August 01, 2012 11:39 PM by carpinteyrorsy

Deus ex petit ami ex petite amie nouveau. Quelque chose d'un qui signifie pauvres qui Santa claus  ainsi que la im ou son, ne peut pas . Heard une lampe de poche? Votre phare? Un article 閘ectronique? Pas un hasard, ensemble Rudolph lueur-nez le patron  responsables. Devenir 閏rou pourrait faire que vous sensei. Zero adolescent sur le globe partout pouvez acheter vient de ce que promotion.  <a href=http://www.chaussuresdefootballpascher.info>chaussures de foot decathlon</a>

Permettez-moi de partager quelques-uns de engrenages:

# re: REST as Simple as Possible

Friday, September 21, 2012 11:35 AM by MabModopupe

Notify the way the life-style regarding Louis XIV together with Louis XVI ended in the undoing inside the This particular language monarchywww.longchamppliagesfr.com

# re: REST as Simple as Possible

Wednesday, October 17, 2012 12:18 AM by Grissom

Hey! Quick question that's completely off topic. Do you know how to make your site mobile friendly? My site looks weird when viewing from my apple iphone. I'm trying

to find a theme or plugin that might be able to

correct this issue. If you have any recommendations, please share.

Appreciate it!

# re: REST as Simple as Possible

Wednesday, October 24, 2012 7:33 AM by Jimmywx3ec

ollvs<a href=> victor cruz jersey </a>

roopq<a href=> donald driver jersey </a>

hcnxk<a href=> brian urlacher jersey </a>

npxxa<a href=> sean lee jersey </a>

ntmax<a href=> sean lee jersey </a>

# re: REST as Simple as Possible

Thursday, March 28, 2013 3:41 PM by Gxlaqnosr

The next group requires counterfeit wrist watches designed to appear like the original genuine watch (the trade-dress violation). Some high-priced counterfeit timepieces are produced from better made materials and possess golden components and leather-based straps.<a href=>inexpensive Wrist watches</a>,<a href=>affordable Watches</a>,<a href=>low-priced Different watches</a>

The Big Pilot's Watch Version Antoine de St . Exupery Perpetual Work schedule is IWC Schaff-hausen's tribute to the existence and function of the renowned Frenchman. Thanks to its transparent sapphire-glass rear cover which shows the particular engraved letters "Edition Saint Exupery" for the rotor and an 18-carat gold medallion with all the initial "A" (regarding Antoine) it is instantaneously recognisable as an unique "Saint Ex". He as soon as wrote: "Perfection obviously does not come up when you've got no more to include but when one can possibly take no more away." It reads almost like a product description with the watch which bears his name. IWC. Built for men.<a href=>affordable Different watches</a>,<a href=>low cost Different watches</a>,<a href=>cheap Watches</a>

Diamond watches are coveted both through men and women around the globe and some of the easiest to capture are simply on a number of amazing public sale or diamond jewelry bidding websites. If you can devote some time out of your day time to escape your 'agony of the feet' and collapse in your easy chair to get some off-market however definitely exquisite, cheap timepieces would you favor that? There are options which will fit your wallet and budget to the central on sites that offer online bidding for cheap watches which have all of the jewelry you were searching for, available each day! Cheap jewellery doesn't have into the future at a minimum, when it comes to, watching individuals designer goods fly from the grasp as well as leaving bare handed and defeated with the check-out lane.<a href=>affordable Different watches</a>,<a href=>low cost Wrist watches</a>,<a href=>low cost Wrist watches</a>

So how can a check out and about dealer manage to get thier genuine leading-of-the-line designer watches at under current market selling prices? According to Essential Watches, it is all about relationships with the crucial "players" and whom they know. This can be the stuff that allows a easy eBay store to increase directly into one of the major look at sellers on the World wide web.<a href=>cheap Wrist watches</a>,<a href=>inexpensive Timepieces</a>,<a href=>inexpensive Wristwatches</a>

Leave a Comment

(required) 
(required) 
(optional)
(required)