1. Trang chủ
  2. » Công Nghệ Thông Tin

Introducing Windows Azure- P46 ppsx

5 275 0
Tài liệu đã được kiểm tra trùng lặp

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 157,69 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

WCF Service Contract Interface Definition of IShoppingCartService using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using

Trang 1

223

passed into the WebInvoke attribute, Method and UriTemplate The value for Method could be either POST or PUT The value of POST is used for inserting or deleting data, and the value of PUT is used for updating data There are four methods defined in this interface:

ƒ PlaceShoppingCartItem()

ƒ UpdateShoppingCartItem()

ƒ DeleteShoppingCartItem()

ƒ PayShoppingCartItem()

Listing 7-5 WCF Service Contract Interface Definition of IShoppingCartService

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

using System.ServiceModel.Web;

using ShoppingCartServiceLibrary;

namespace ShoopingCartWorkflows

{

[ServiceContract]

public interface IShoppingCartService

{

[OperationContract]

[WebInvoke(Method="POST", UriTemplate="ShoppingCartItem")]

ShoppingCartItem PlaceShoppingCartItem(ShoppingCartItem ShoppingCartItem);

[OperationContract]

[WebInvoke(Method = "PUT", UriTemplate = "ShoppingCartItem/{id}")]

ShoppingCartItem

UpdateShoppingCartItem(string id, ShoppingCartItem ShoppingCartItem);

[OperationContract]

[WebInvoke(Method = "POST", UriTemplate = "ShoppingCartItem/{id}")]

ShoppingCartItem

DeleteShoppingCartItem(string id, ShoppingCartItem ShoppingCartItem);

[OperationContract]

[WebInvoke(Method="PUT",

UriTemplate="CreditCardPayment/ShoppingCartItem/{id}")]

void PayShoppingCartItem(string id, CreditCardPayment CreditCardPayment);

}

}

Trang 2

224

3 Add a C# library project ShoppingCartServiceLibrary to implement the IShoppingCartService service contract and data contract as shown in Listing 7-6 and Listing 7-7 Listing 7-6 is the implementation for the service interface, and Listing 7-7 is the data contract used to hold the shopping card property data

Listing 7-6 WCF IShoppingCartService Service Contract Implementation

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Runtime.Serialization;

namespace ShoppingCartServiceLibrary

{

[Serializable]

[DataContract(Name="ShoppingCartItem",

Namespace="http://costco.com/OnlineShopping")]

public class ShoppingCartItem

{

static public string ENDPOINT CREDITCARD PAYMENT =

"http://costco.com/OnlineShopping/CreditCardPayment/";

static public string ENDPOINT ITEM UPDATE =

"http://costco.com/OnlineShopping/ShoppingCartItem/update/"; static public string ENDPOINT ITEM DELETE =

"http://costco.com/OnlineShopping/ShoppingCartItem/delete/"; static public string CREDIT CARD PAYMENT URI =

"http://localhost:8000/CreditCardPayment/ShoppingCartItem/"; static public string SHOPPING CART URI =

"http://localhost:8000/ShoppingCartItem/";

public string ShoppingCartItemId { get; set; }

[DataMember(Name="ItemName")]

public string ItemName { get; set; }

[DataMember(Name="Price")]

public decimal? Price { get; set; }

[DataMember(Name="NextItem")]

public NextItem[] NextItem { get; set; }

}

[Serializable]

[DataContract(Name="NextItem", Namespace="http://costco.com/OnlineShopping")] public class NextItem

Trang 3

225

{

[DataMember(Name="Relative")]

public string Relative { get; set; }

[DataMember(Name="uri")]

public string Uri { get; set; }

[DataMember(Name="type")]

public string Type { get; set; }

public NextItem()

{

Type = "application/xml";

}

}

}

Listing 7-7 WCF IShoppingCartService Service Data Contract Implementations

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Runtime.Serialization;

namespace ShoppingCartServiceLibrary

{

[Serializable]

[DataContract(Name = "CreditCardPayment",

Namespace = "http://costco.com/OnlineShopping")]

public class CreditCardPayment

{

public string ShoppingCartItemId { get; set; }

[DataMember(Name = "CardNumber")]

public string CardNumber { get; set; }

[DataMember(Name = "ExpiresDate")]

public string ExpiresDate { get; set; }

[DataMember(Name = "CardHolerName")]

public string CardHolerName { get; set; }

[DataMember(Name = "amount")]

public decimal ChargedAmount { get; set; }

}

}

Trang 4

226

4 Go back to the State Machine Workflow design surface and drag and drop four state artifacts onto the design surface and name them WaitingForShoppingCartItem, ShoppingCartItemPlaced, ItemCheckOut, and CartClosed, as shown in Figure 7-8 The process to work out the logic of state transaction is the same as the process to work on the standard WF state machine Please see http://msdn.microsoft.com/ en-us/netframework/aa663328.aspx to understand Microsoft Windows Workflow Foundation and how to work on the WF state machine design

in Visual Studio

Figure 7-8 States defined in ShoppingCartWorkflow state machine

5 Now let's build up the state machine used to handle a shopping cart service Implement a code-behind for the ShoppingCartItemWorkflow state machine as shown in Listing 7-8 The event handler functions in the code-behind will be bound to events in the state machine during the next state machine design This shopping cart example has four state transaction handlers from the code-behind These handlers are generated from the WF design surface; insert the code from the listing into the handlers’ bodies (Right-click the design surface and select View Code, or press F7, to get into the state transaction handler body.) There

is an array called NextItem defined in each handler, which is the data contract of the WCF services in this example The member items in the array also have the type of NextItem that is used to simulate the business activities, such as shopping cart data insert, update, or delete The state will be transacted depending on the activities in the handler

Trang 5

227

Listing 7-8 Code-behind for ShoppingCartWorkflow

using System;

using System.ComponentModel;

using System.ComponentModel.Design;

using System.Collections;

using System.Drawing;

using System.Linq;

using System.Workflow.ComponentModel.Compiler;

using System.Workflow.ComponentModel.Serialization;

using System.Workflow.ComponentModel;

using System.Workflow.ComponentModel.Design;

using System.Workflow.Runtime;

using System.Workflow.Activities;

using System.Workflow.Activities.Rules;

using System.ServiceModel.Web;

using ShoppingCartServiceLibrary;

using System.ServiceModel;

namespace ShoppingCartWorkflows

{

using ShoppingCartServiceLibrary;

public sealed partial class ShoppingCartItemWorkflow : StateMachineWorkflowActivity

{

public ShoppingCartItem receivedShoppingCartItem;

public string receivedId;

public ShoppingCartItem currentShoppingCartItem;

public CreditCardPayment ShoppingCartItemCreditCardPayment;

public ShoppingCartItemWorkflow()

{

InitializeComponent();

}

private void OnShoppingCartItemPlacedCode ExecuteCode(object sender, EventArgs e)

{

var id = WorkflowEnvironment.WorkflowInstanceId.ToString();

currentShoppingCartItem = new ShoppingCartItem();

currentShoppingCartItem.ShoppingCartItemId = id;

currentShoppingCartItem.Price = receivedShoppingCartItem.Price;

currentShoppingCartItem.ItemName = receivedShoppingCartItem.ItemName;

currentShoppingCartItem.NextItem = new NextItem[]

{

new NextItem {

Relative = ShoppingCartItem.ENDPOINT CREDITCARD PAYMENT,

Uri = string.Format("{0}{1}",

ShoppingCartItem.CREDIT CARD PAYMENT URI,

WorkflowEnvironment.WorkflowInstanceId.ToString()),

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