RESTful WCF Part 1 of n

Lately I have been neglecting my  coding duties a bit in order to go out and get drunk actually. Which I have become a bit tired of now, so I am back onto the code, which is a good place.

Now I am in the process of creating another article, and though I can not reveal what I am working on, as there is a competition involved, which I would like to have a pop at, I thought I may share bits of information about parts of it along the way.

I can say that I am using some new WCF stuff that came with .NET 3.5, namely the System.ServiceModel.Web namespace which allows us to create RESTful WCF services.

 

For those that do not know what REST is, here is what Wikipedia has to say about it.

 

"Representational state transfer (REST) is a style of software architecture for distributed hypermedia systems such as the World Wide Web. As such, it is not strictly a method for building "web services." The terms "representational state transfer" and "REST" were introduced in 2000 in the doctoral dissertation of Roy Fielding,[1] one of the principal authors of the Hypertext Transfer Protocol (HTTP) specification.

REST strictly refers to a collection of network architecture principles which outline how resources are defined and addressed. The term is often used in a looser sense to describe any simple interface which transmits domain-specific data over HTTP without an additional messaging layer such as SOAP or session tracking via HTTP cookies. These two meanings can conflict as well as overlap. It is possible to design a software system in accordance with Fielding’s REST architectural style without using HTTP and without interacting with the World Wide Web.[2] It is also possible to design simple XML+HTTP interfaces which do not conform to REST principles, and instead follow a model of remote procedure call. The difference between the uses of the term "REST" therefore causes some confusion in technical discussions.

Systems which follow Fielding’s REST principles are often referred to as "RESTful"."

http://en.wikipedia.org/wiki/Representational_State_Transfer

 

Basically what it means as a WCF developer is that you will expose resources which are accessible via Urls, which means all a client needs to use your service is a browser.

 

The schedule of posts is probably going to be something like this

  1. The new RESTful WCF attributes (this article)
  2. Serialization options
  3. Hosting
  4. CRUD operations using RESTful WCF

 

Ok so lets get on to some code.

This blog post will simply cover the new attributes that enable RESTful WCF.

In fact there is really only 2 attributes that in my mind are the lynchpins for the whole RESTful way of doing things using WCF. These attributes are 

WebGetAttribute

"The WebGetAttribute attribute is applied to a service operation in addition to the OperationContractAttribute and associates the operation with a UriTemplate as well as the HTTP protocol Get verb. The association with HTTP Get verb means that the operation is used to retrieve information from the service. The WebGetAttribute attribute is a passive operation behavior (the IOperationBehavior methods do nothing) that adds metadata to the operation description. Applying the WebGetAttribute attribute to a service operation has no effect unless a behavior that is looking for this metadata in the operation description (such as WebHttpBehavior) is added to the service’s behavior collection."

MSDN

 

WebInvokeAttribute

"The WebInvokeAttribute attribute is applied to a service operation in addition to the OperationContractAttribute and associates the operation with a UriTemplate as well as an underlying transport verb that represents an invocation (for example, HTTP POST, PUT, or DELETE). The WebInvokeAttribute attribute is a passive operation behavior (the IOperationBehavior methods do nothing) that adds metadata to the operation description. Applying the WebInvokeAttribute attribute to a service operation has no effect unless a behavior that looks for this metadata in the operation description (such as WebHttpBehavior) is added to the service’s behavior collection. The WebInvokeAttribute determines what HTTP method that a service operation responds to. By default, all methods that have the WebInvokeAttribute applied respond to POST requests. The Method property allows you to specify a different HTTP method. If you want a service operation to respond to GET, use the WebGetAttribute instead."

MSDN

Let us now see some WCF code that uses one of these attributes. I am going to only discuss the usage of the WebGetAttribute in this post as I do not want to talk about all the CRUD operations that can be done with RESTful WCF just yet.

 

So in order to expose resources over a Url, we can use the WebGetAttribute  to adorn our WCF service operations, here is an example

   1:  [ServiceContract(SessionMode = 
   2:      SessionMode.NotAllowed)]
   3:  public interface ISomeService
   4:  {
   5:   
   6:      [OperationContract]
   7:      [WebGet(UriTemplate = "/")]
   8:      Message GetRoot();
   9:   
  10:      [OperationContract]
  11:      [WebGet(UriTemplate = "/{userName}/")]
  12:      Message GetFavouriteBarsForUser(
  13:          String userName);
  14:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Notice that I have used the WebGetAttribute to adorn the WCF OperationContract methods. Also notice that there is a UriTemplate property which is set to either a root terminator "/" or has some more parts to the Url, where the extra parts of the Url are treated as method parameters, which allow more specific resources to be exposed.

In essence that is all there is to allowing a service to expose resources. Now we need to test this. In order to do that we need to implement this example service, so lets have a look at a small demo app I knocked up.

 

   1:  [ServiceBehavior(IncludeExceptionDetailInFaults = false, 
   2:      InstanceContextMode = InstanceContextMode.Single, 
   3:      ConcurrencyMode = ConcurrencyMode.Single)]
   4:  public class SomeService : ISomeService
   5:  {
   6:   
   7:      #region ISomeService Members
   8:   
   9:      public Message GetRoot()
  10:      {
  11:   
  12:          Message message = GetRootMessage();
  13:          return message;
  14:      }
  15:   
  16:   
  17:   
  18:      public Message 
  19:          GetFavouriteBarsForUser(string userName)
  20:      {
  21:          Message message = 
  22:              GetFavouriteBarsForUserMessage(userName);
  23:          return message;
  24:      }
  25:   
  26:      #endregion
  27:   
  28:      #region Private Methods
  29:   
  30:      /// <summary>
  31:      /// Create bars for User message
  32:      /// </summary>
  33:      private Message 
  34:          GetFavouriteBarsForUserMessage(String userName)
  35:      {
  36:          var stream = new MemoryStream();
  37:          XmlDictionaryWriter writer = 
  38:              XmlDictionaryWriter.CreateTextWriter(stream);
  39:          writer.WriteStartDocument();
  40:          writer.WriteStartElement("Root");
  41:          writer.WriteStartElement("Bar");
  42:          writer.WriteElementString("user", userName);
  43:          writer.WriteEndElement();
  44:          writer.WriteEndElement();
  45:          writer.WriteEndDocument();
  46:          writer.Flush();
  47:          stream.Position = 0;
  48:   
  49:          XmlDictionaryReader reader = 
  50:              XmlDictionaryReader.CreateTextReader(stream, 
  51:              XmlDictionaryReaderQuotas.Max);
  52:          return Message.CreateMessage(
  53:              MessageVersion.None, "", reader);
  54:      }
  55:   
  56:   
  57:      /// <summary>
  58:      /// Create root message
  59:      /// </summary>
  60:      private Message GetRootMessage()
  61:      {
  62:          var stream = new MemoryStream();
  63:          XmlDictionaryWriter writer = 
  64:              XmlDictionaryWriter.CreateTextWriter(stream);
  65:          writer.WriteStartDocument();
  66:          writer.WriteStartElement("Root");
  67:          writer.WriteStartElement("Hello");
  68:          writer.WriteElementString("Name", "sacha");
  69:          writer.WriteEndElement();
  70:          writer.WriteEndElement();
  71:          writer.WriteEndDocument();
  72:          writer.Flush();
  73:          stream.Position = 0;
  74:   
  75:          XmlDictionaryReader reader = 
  76:              XmlDictionaryReader.CreateTextReader(
  77:              stream, XmlDictionaryReaderQuotas.Max);
  78:          return Message.CreateMessage(
  79:              MessageVersion.None, "", reader);
  80:      }
  81:      #endregion
  82:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

So you can see for each of the WebGetAttribute adorned WCF OperationContract methods, we will return a Message, which is really just some XML, which browsers will know how to display. If you don’t know what a Message Type is, do not worry we will be covering that in a subsequent post.

For now the important thing to note is that the service is exposing resources over REST using Urls.

The attached demo code includes a simply host, which when run will allow you to test various REST calls.

Here is what you need to do

  1. Run the simple host (SomeServiceTestConsole.exe)
  2. Open a browser and try the following Urls
    1. http://localhost:8085/SomeService
    2. http://localhost:8085/SomeService/sacha
    3. http://localhost:8085/SomeService/peter

 

And here is what you should see

http://localhost:8085/SomeService

image

 

http://localhost:8085/SomeService/sacha

 

image

 

I hope you can see that by using these new RESTful attributes we can expose resources via Urls, which is accessable simply using a Browser. Of course to parse the XML there are many options.

 

Here is a small demo app that has a very simple RESTful service and a very simple Console host.

simplerestful_getexample.zip

 

We will continue the journey next time

Advertisements

16 thoughts on “RESTful WCF Part 1 of n

  1. […] RESTful WCF Part 1 of n (Sacha Barber) […]

  2. Nice post Sacha. You are showing with more details some of the things I use in my MIX talk. Very interesting. I look forward to the rest of the posts.

  3. sacha says:

    Thanks Laurent, I hope the next post or 2 and the article will show some fun stuff

  4. Raul Mainardi Neto says:

    Nice work Sacha, as usual, looking forward to read the rest.

    All best mate

    Raul

  5. sacha says:

    Thanks Raul.

  6. cowgaR says:

    will you focus on JSON serialization in the future articles?

    I hope it is not only my personal preference…

    Thanks

  7. sacha says:

    cowgaR

    I may do that, but I may not, Ill see how much time I have free.

    I more talking xml/datacontract and Message serialization, but I may go into JSON also.

  8. Michael says:

    I would also be interested to know the options for JSON. I’m hoping the REST toolkit has a way to return it as either JSON or xml by using some kind of switch in the url.

  9. Marlon Grech says:

    very interesting dude. I like 🙂

  10. sacha says:

    Michael,

    Thats exactly what happens. The next post will show this.

    It will be very soon.

  11. sacha says:

    Marlon, thanks man

  12. […] when) I started saying I was going to write a bit about RESTFul WCF, you can see my 1st post at http://sachabarber.net/?p=460. In that post I mentioned that I would probably do the […]

  13. http://sell-home-loans.floridahousinginfo.com/ 497 mhf [url=http://sell-home-loans.floridahousinginfo.com/] 497 mhf [/url] http://credit-score-3-1.floridahousinginfo.com/ 7rj vmb [url=http://credit-score-3-1.floridahousinginfo.com/] 7rj vmb [/url] http://construction-repair-and-modernization-by-insuring-loans-and-mortgages.floridahousinginfo.com/ 9rf 4sg [url=http://construction-repair-and-modernization-by-insuring-loans-and-mortgages.floridahousinginfo.com/] 9rf 4sg [/url] http://loan-brokers-license.floridahousinginfo.com/ 0zh jg2 [url=http://loan-brokers-license.floridahousinginfo.com/] 0zh jg2 [/url] http://government-loans-and.floridahousinginfo.com/ 4cm 5fq [url=http://government-loans-and.floridahousinginfo.com/] 4cm 5fq [/url] http://credit-first-time-home-buyers.floridahousinginfo.com/ yrv 86h [url=http://credit-first-time-home-buyers.floridahousinginfo.com/] yrv 86h [/url] http://parents-plus-college-loans.floridahousinginfo.com/ nh9 drg [url=http://parents-plus-college-loans.floridahousinginfo.com/] nh9 drg [/url] http://parents-college-loan.floridahousinginfo.com/ vdg prv [url=http://parents-college-loan.floridahousinginfo.com/] vdg prv [/url] http://college-loans-payment.floridahousinginfo.com/ 167 1v4 [url=http://college-loans-payment.floridahousinginfo.com/] 167 1v4 [/url] http://adjustable-rate-debt.floridahousinginfo.com/ jgy 9mv [url=http://adjustable-rate-debt.floridahousinginfo.com/] jgy 9mv [/url] http://www.youtube.com/watch?v=WiPM2M8RavY cheap auto insurance [url=http://www.youtube.com/watch?v=WiPM2M8RavY] cheap auto insurance [/url] http://www.metacafe.com/watch/2758477/insrance_quotes_online_auto_home_life_health_other/ cheap car insurance [url=http://www.metacafe.com/watch/2758477/insrance_quotes_online_auto_home_life_health_other/] cheap car insurance [/url] http://www.youtube.com/watch?v=CAcdtXAk_Xw car insurance [url=http://www.youtube.com/watch?v=CAcdtXAk_Xw] car insurance [/url] maxim-1980@bk.ru

  14. Agasi says:

    Thank you very much!!!very very helpful article

  15. Agasi says:

    I am reading your articles and they are very helpful,they helped me a lot to start my restful web service.
    Can you please tell me how to get the Asterisk “*” as part of the variable like this:
    “http://localhost:8085/SomeService/{variable}”
    which ‘variable’ could contain Asterisk “*”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: