Introducing Windows Azure- P49 docx

5 270 0
Introducing Windows Azure- P49 docx

Đang tải... (xem toàn văn)

Thông tin tài liệu

CHAPTER 7 ■ AZURE .NET SERVICES—WORKFLOWS 238 Figure 7-14. Use the SQL scripts provided by the .NET 3.0 Windows Workflow Foundation to set up the persistent SQL data table from the local development environment Create a console application as a test client for testing. This test application instantiates a unit test library class InvokeWorkflows and calls InvokeHttpWebRequest. In this test client, we use HttpWebRequest to post or put a message via HTTP with the following steps to trigger the workflow state machine transaction from one state to another. Note that the client does not need to reference the System.Workflow.Activities assembly at all. That is why we can deploy an application that uses this approach to the cloud without dealing with authentication security trust issues. The sample client code is shown in Listing 7-11, and the following steps are the actions to simulate a shopping cart business flow. 1. Create a ShoppingCartItem object and call HttpWebRequest to post data to the workflow server. This should advance the workflow state machine from the initial state of waiting for shopping cart item state to shopping cart item place state. 2. Update the contents of the ShoppingCartItem and call HttpWebRequest to update the data in the shopping cart. The state in this activity should loop back to the same state by design, and the workflow state machine continues to wait for the next event. 3. Create a credit card payment object and call HttpWebRequest to trigger the state machine to move to the check out state. The workflow state machine should finish at the shopping card closed state. Listing 7-11. Client Code Using HttpWebRequest to Invoke the State Machine Workflow using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; using System.Runtime.Serialization; using System.ServiceModel; CHAPTER 7 ■ AZURE .NET SERVICES—WORKFLOWS 239 using System.ServiceModel.Channels; using System.ServiceModel.Web; namespace ClientApplication { using ShoppingCartServiceLibrary; class Program { static void Main(string[] args) { //Create a new ShoppingCartItem ShoppingCartItem shoppingCartItem = new ShoppingCartItem { ItemName = "Nikon Camera", Price = (decimal)1000.0 }; HttpWebRequest createShoppingCartItemRequest = CreateRequest(new Uri("http://localhost:8000/ShoppingCartItem"), "POST", shoppingCartItem); shoppingCartItem = InvokeHttpWebRequest<ShoppingCartItem>( createShoppingCartItemRequest.GetResponse() ); Console.WriteLine("ShoppingCartItem Price {0}", shoppingCartItem.Price); foreach (NextItem NextItem in shoppingCartItem.NextItem) { Console.WriteLine("Check NextItem {0}", NextItem.Uri); } //Update shoppingCartItem.ItemName = "Dell latiture D620"; shoppingCartItem.Price = (decimal)710.0; Uri updateShoppingCartItemUri = new Uri(shoppingCartItem.NextItem.Where( n => n.Relative == ShoppingCartItem.ENDPOINT ITEM UPDATE).Single().Uri); HttpWebRequest updateShoppingCartItemRequest = CreateRequest(updateShoppingCartItemUri, "PUT", shoppingCartItem); ShoppingCartItem updatedShoppingCartItem = InvokeHttpWebRequest<ShoppingCartItem>( updateShoppingCartItemRequest.GetResponse() ); Console.WriteLine("ShoppingCartItem Price {0}", updatedShoppingCartItem.Price); foreach (NextItem NextItem in updatedShoppingCartItem.NextItem) { Console.WriteLine("Process NextItem {0}", NextItem.Uri); } CHAPTER 7 ■ AZURE .NET SERVICES—WORKFLOWS 240 //Pay CreditCardPayment CreditCardPayment = new CreditCardPayment { CardHolerName = "David Smith", CardNumber = "1234567, ExpiresDate = "10/10", ChargedAmount = updatedShoppingCartItem.Price.GetValueOrDefault() }; Uri payShoppingCartItemUri = new Uri(shoppingCartItem.NextItem.Where( n => n.Relative == ShoppingCartItem.ENDPOINT CREDITCARD PAYMENT).Single().Uri); HttpWebRequest payShoppingCartItemRequest = CreateRequest(payShoppingCartItemUri, "PUT", CreditCardPayment); int statusCode = Execute(payShoppingCartItemRequest.GetResponse()); if (statusCode == 201) Console.WriteLine("Transaction success!"); else Console.WriteLine("Failed to process the CreditCardPayment"); //Checkout Uri deleteShoppingCartItemUri = new Uri(shoppingCartItem.NextItem.Where( n => n.Relative == ShoppingCartItem.ENDPOINT ITEM DELETE).Single().Uri); HttpWebRequest deletehoppingCartItemRequest = CreateRequest(deleteShoppingCartItemUri, "POST", shoppingCartItem); ShoppingCartItem deletedShoppingCartItem = InvokeHttpWebRequest<ShoppingCartItem>( deletehoppingCartItemRequest.GetResponse() ); } static HttpWebRequest CreateRequest(Uri address, string method, object contract) { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(address); webRequest.ContentType = "application/xml"; webRequest.Timeout = 20000; webRequest.Method = method; DataContractSerializer serializer = new DataContractSerializer(contract.GetType()); using (Stream stream = webRequest.GetRequestStream()) { serializer.WriteObject(stream, contract); stream.Flush(); } CHAPTER 7 ■ AZURE .NET SERVICES—WORKFLOWS 241 return webRequest; } static T InvokeHttpWebRequest<T>(WebResponse response) { DataContractSerializer serializer = new DataContractSerializer(typeof(T)); using (Stream stream = response.GetResponseStream()) { return (T)serializer.ReadObject(stream); } } static int InvokeHttpWebRequest (WebResponse response) { using (Stream stream = response.GetResponseStream()) { }; return (int)((HttpWebResponse)response).StatusCode; } } } Start the host service to listen to the HTTP web request. When the service host starts, it listens to the HTTP web request. Press <enter> key to exit Run the test client project. The test results show that the workflow state machine goes through all states, ending with the shopping cart state: Press <enter> key to exit WorkflowCompleted. Create a cloud service HostWCFServiceInCloud with a WorkflowsClient WorkerRole and invoke the state machine workflows from the local cloud fabric. We have the same results as previously. This demonstrates that with the help of HttpWebRequest we can invoke the state machine workflows from the cloud to work around the limitation from the current Azure .NET service state machine workflow. The code for the worker role is shown in Listing 7-12. Listing 7-12. Invoke State Machine Workflows from the Cloud Using Worker Role using System; using System.Collections.Generic; using System.Threading; using System.Linq; using System.Text; using Microsoft.ServiceHosting.ServiceRuntime; using System.Runtime.Serialization; CHAPTER 7 ■ AZURE .NET SERVICES—WORKFLOWS 242 using System.ServiceModel; using System.ServiceModel.Channels; using System.ServiceModel.Web; using System.Net; using System.IO; namespace WorkflowsClient WorkerRole { using ClientUnitestibrary; public class WorkerRole : RoleEntryPoint { public override void Start() { RoleManager.WriteToLog("Information", "Worker Process entry point called"); InvokeWorkflows invokeWorkflows = new InvokeWorkflows(); invokeWorkflows.HttWebRequestInvokeWorkflows(); while (true) { Thread.Sleep(10000); RoleManager.WriteToLog("Information", "Working"); } } public override RoleStatus GetHealthStatus() { return RoleStatus.Healthy; } } } Summary In this chapter we covered workflows and how to host them in the cloud. We started with an example of a sequential workflow service and saw how easy it was to use in Azure. The workflow service was essentially the same as regular .NET workflow, with the exception that we could not use any code- behind files. Our second example provided a work-around to a limitation in the .NET Workflow service: namely that we can't deploy state machine or custom workflows to the cloud. Our example used HttpWebRequest to switch the states of a state machine workflow to demonstrate the work-around.f . AZURE .NET SERVICES—WORKFLOWS 238 Figure 7-14. Use the SQL scripts provided by the .NET 3.0 Windows Workflow Foundation to set up the persistent SQL data table from the local development

Ngày đăng: 05/07/2014, 01:20

Tài liệu cùng người dùng

  • Đang cập nhật ...