RESTing with the Microsoft REST Starter Kit
Service Type
I copied most of the PlainXML Service Type code from an application created from the "HTTP Plain XML WCF Service" template. I only made changes to some of the hosting attributes on the Service Type. Code implementing an HTTP POST to http:// localhost:8000/TestServiceHost/DoWork appears below:
[WebHelp(Comment = "Sample description for DoWork")] [WebInvoke(UriTemplate = "DoWork")] [OperationContract] public SampleResponseBody DoWork(SampleRequestBody request) { return new SampleResponseBody() { Value = String.Format("Sample DoWork response: '{0}'", request.Data) }; }
The WebInvoke attribute maps any POST
to the
"DoWork" resource to the code above and WebHelp controls
some of what is displayed by a GET
to the
"Help" resource. GetData
follows a similar
pattern as POST, only the attributes and returned
information differ.
Like much of WCF, the REST Starter Kit relies on DataContractSerialization. So when the POST executes the runtime Deserializes to a SampleRequestBody object and then Serializes a SampleResponseBody before transmission to the Client.
HttpClient
Things are a little more exciting on the Client side. Main parts of the Client side code appear below with Console Writeline statements removed for readability.
string baseUri = "http://localhost:8000/TestServiceHost/"; HttpClient http = new HttpClient(baseUri); SampleRequestBody request = new SampleRequestBody(); HttpResponseMessage resp; SampleResponseBody respBody = null; http.Stages.Add(new StageProcessingTest()); request.Data = "My request..."; resp = http.Post("DoWork", HttpContentExtensions.CreateDataContract<SAMPLEREQUESTBODY>(request)); if (resp.StatusCode == System.Net.HttpStatusCode.OK) { respBody = resp.Content.ReadAsDataContract<SAMPLERESPONSEBODY>(); }
HttpClient houses most of the client-side functionality. Like other parts of the .NET Framework, there are two ways to utilize the library:
- Synchronously where the thread blocks until the call returns
- Asynchronously where the thread executes elsewhere and the .NET Runtime makes a callback to a supplied delegate when the request is completed or fails.
I'll review the Synchronous functionality first and the cover the Asynchronous functionally later in the article.
Post, Get, Delete
, and Put
are all synchronous methods on
the HttpClient
object. I do a Post in the example, but the
other operations work in a similar fashion.
Post returns an HttpResponseMessage
containing two pieces
of information.
A status of the HTTP operation, so rather than decoding each HTTP status code to determine success or failure the Starter Kit does the decoding for you. If you do want to look at the nitty-gritty response codes the response message includes the details.
Data is returned in the Content Property.
HttpContentExtensions
provides functions to push the data
into .NET objects or manipulate the raw byte stream. In the
example, I'm Deserializing the content into a
SampleResponseBody
function.
Page 2 of 3
This article was originally published on June 22, 2009