Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Azure

A REST Service in Windows Azure

4/16/2011 3:34:48 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
In order to explore how REST services are created and exist within Windows Azure, this section takes the Web service from the previous section and makes it RESTful. But, before we dive into the implementation details of this change, let’s first take a step back and think about REST-specific design considerations.

REST Service Addressing

A common design practice with REST services is to make the addressing (the manner in which target resources are addressed) as intuitive as possible. The social bookmarking site Delicious is a great example of this.

With Delicious, every bookmark has one or more tags (think of tags as categories). Tags essentially replace folders within Web browsers with categories. In relation to our discussion, you can also group tags into a bundle, which basically creates “tag clouds.” Access to tagged bookmarks is provided via REST services. Table 1 shows a set of sample URLs that can be used to get back a list of bookmarks for Azure, SOA, and Azure+SOA, respectively.

Table 1. Sample URLs used to retrieve different values from REST services at delicious.com.
URLDescription
http://delicious.com/tag/azurereturns a list of bookmarks that have been tagged with Azure
http://delicious.com/tag/soareturns a list of bookmarks that have been tagged with SOA
http://delicious.com/tag/soa+azurereturns a list of bookmarks that have been tagged with SOA and Azure

What’s important about this example is that we are able to search, create and update a large network of data via REST without writing code. The HTTP GET method and the appropriate URLs are all we need.

Returning to our Order service, we first need to define an appropriate resource addressing structure for the order data, as shown in Table 2.

Table 2. The resource addressing structure for the Order service.
ActionIOrderService Operation NameURI Address TemplateHTTP Method
get a list of all ordersGetOrders./ordersGET
get an order given the order IDGetOrderByOrderId./order/{id}GET
get a list of orders for a given customerGetOrdersByCustomer./orders/{custName}GET
create an orderCreateOrder./ordersPOST
update an orderUpdateOrder./order/{id}PUT
delete an orderDeleteOrder./order/{id}DELETE

Creating a Windows Azure REST Service

We now need to carry out a series of steps to make this a REST service:

1.
Add a reference to System.ServiceModel.Web in the Contract project.

2.
Add HTTP attributes to the methods defined in the IOrderService interface.

3.
Update the WCF behavior.

4.
Update the OrderService.svc file by adding a Web factory reference.

The System.ServiceModel.Web namespace contains classes that make up the Web HTTP programming model.

For our purposes, we need to focus on the following:

  • WebGetAttribute (maps to an HTTP GET)

  • WebInvokeAttribute (maps to HTTP POST, PUT, and DELETE)

  • WebMessageFormat (defines the format of the response message)

For the GET method, we use the WebGet attribute. We then use the UriTemplate attribute to define the addressing structure from Table 2. This is a manual process, which means that it’s easy to make mistakes. It is therefore important to lay out the URI structure prior to working with the code.

We also need to specify the {token} parameters. For example, if we were calling the GetOrderByOrderId operation of the Web service via SOAP, we would just pass in the order ID argument by calling the Web method. But with REST, everything is through HTTP methods and URIs. The service consumer doesn’t call GetOrderByOrderId directly, but rather does the HTTP GET method on http://server/OrderService.svc/order/2, where “2” is the order ID value.

Next, we need to determine the response message format by setting ResponseFormat to return XML messages.

Here’s what IOrderService looks like now:

Example 1.
 [ServiceContract]
public interface IOrderService
{
[WebInvoke(Method="POST",
UriTemplate="orders",
ResponseFormat=WebMessageFormat.Xml)]
[OperationContract]
int CreateOrder(Order o);
[WebInvoke(Method="PUT",
UriTemplate="order/{id}",
ResponseFormat=WebMessageFormat.Xml)]
[OperationContract]
void UpdateOrder(string id, Order o);
[WebGet(UriTemplate="order/{id}",
ResponseFormat=WebMessageFormat.Xml)]
[OperationContract]
Order GetOrderByOrderId(string id);
[WebGet(UriTemplate="orders/{custName}",
ResponseFormat=WebMessageFormat.Xml)]
[OperationContract]
List<Order> GetOrdersByCustomer(string custName);
[WebGet(UriTemplate="orders",
ResponseFormat=WebMessageFormat.Xml)]
[OperationContract]
List<Order> GetOrders();
[WebInvoke(Method="DELETE",
UriTemplate="order/{id}",
ResponseFormat=WebMessageFormat.Xml)]
[OperationContract]
void DeleteOrder(string id);
}


We now need to update the WCF behavior in the Web.Config file by changing the endpoint binding to WebHttpBinding and the endpoint behavior to a Web behavior, as shown here:

Example 2.
<services>
<servicebehaviorConfiguration=
"ServiceDemo_WebRole.ServiceDemoBehavior"
name="ServiceDemo_WebRole.OrderService">
<endpoint address="" binding="WebHttpBinding"
contract="Contract.IOrderService"
behaviorConfiguration="Web">
</endpoint>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="Web" />
</endpointBehaviors>
<serviceBehaviors>
<behavior name=
"ServiceDemo_WebRole.ServiceDemoBehavior">
</serviceBehaviors>
</behaviors>

Finally, we have to update the OrderService.svc file to include WebServiceHostFactory, as shown here:

Example 3.
<%@
ServiceHost Language="C#" Debug="true"
Service="ServiceDemo_WebRole.OrderService"
CodeBehind="OrderService.svc.cs"
Factory="System.ServiceModel.
Activation.WebServiceHostFactory"
%>

WebServiceHostFactory provides instances of WebServiceHost in managed hosting environments, where the host instance is created dynamically in response to incoming messages. This is necessary because the service is being hosted using a Web role in IIS.

Other -----------------
- Cloud Services with Windows Azure : A Web Service in Windows Azure
- Cloud Services with Windows Azure : Hello World in Windows Azure
- Cloud Services with Windows Azure : Windows Azure Roles
- Cloud Services with Windows Azure : Windows Azure Platform Overview
- Cloud Services with Windows Azure : Cloud Computing 101
- SOA with .NET and Windows Azure : Orchestration Patterns with WF - Compensating Service Transaction
- SOA with .NET and Windows Azure : Orchestration Patterns with WF - State Repository
- SOA with .NET and Windows Azure : Orchestration Patterns with WF - Process Centralization
- SOA with .NET and Windows Azure : Process Abstraction and Orchestrated Task Services (part 4) - Publishing WF Workflows as REST Services
- SOA with .NET and Windows Azure : Process Abstraction and Orchestrated Task Services (part 3)
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server