ườ

Tr

ọ ự ng ĐH Khoa H c T  Nhiên TP.HCM

TRUNG TÂM TIN H CỌ

L p trình Windows Phone Module 4 – Bài 3­4: WCF & Cloud

GV Biên so n: Tr n Duy Thanh

2014

Nội dung

• WCF – Windows Communication Foundation

• Windows Azure Mobile Services

2

WCF & Cloud

1. Windows Communication Foundation • Khái niệm WCF

• Kiến trúc WCF

• Các thuộc tính thường sử dụng

• Cách tạo WCF

• Windows Phone và WCF

3

WCF & Cloud

Khái niệm WCF

Ø WCF là công nghệ nền tảng nhằm thống nhất nhiều mô hình lập trình giao tiếp được hỗ trợ trong .NET 2.0 thành một mô hình duy nhất.

Ø WCF có thể sử dụng các bản tin SOAP giữa hai tiến trình, do đó làm cho các ứng dụng dựa trên WCF có thể làm việc với các tiến trình khác thông qua việc giao tiếp sử dụng bản tin SOAP.

Ø Khi một tiến trình WCF liên lạc với một tiến trình không là WCF, các bản tin SOAP được mã hoá trên cơ sở XML, nhưng khi nó liên lạc với một tiến trình WCF khác, bản tin SOAP có thể được tối ưu hoá dựa trên mã hoá nhị 4 WCF & Cloud phân.

Kiến trúc WCF

5

WCF & Cloud

Kiến trúc WCF

6

WCF & Cloud

Kiến trúc WCF

WebService WCF

ử ụ S   d ng ộ thu c tính ử S ụ d ng ộ thu c tính

ể [WebService] trong class ế [ServiceContract] đ  thay th .

ử ụ ử ụ S   d ng [WebMethod] S   d ng [OperationContract]

ớ v i Method. ớ v i Method

ể ỉ ể Ch  có th  host trên IIS Có  th   host  trên  IIS,  WAS

(Windows  Activation  Service),

ộ ặ trong m t Window Service ho c

7

host chính nó.

ự t WCF & Cloud

Các thuộc tính thường sử dụng• Service Contract

• Operation Contract

• Data Contract

• DataMember

• Fault Contracts

8

WCF & Cloud

Cách tạo WCF

9

WCF & Cloud

Cách tạo WCF

10

WCF & Cloud

Cách tạo WCF

Dùng WAS:

private ServiceHost host; private void btnStartServer_Click(object sender,  EventArgs e)        {  String add = "http://169.254.80.80/Test";  host = new ServiceHost(typeof(Test), new Uri(add));  ServiceMetadataBehavior sv = new  ServiceMetadataBehavior();

sv.HttpGetEnabled = true;   sv.MetadataExporter.PolicyVersion =

PolicyVersion.Policy15;

11

WCF & Cloud

host.Description.Behaviors.Add(sv);    host.Open();                 }

Windows Phone và WCF

Sử dụng WCF Test Client để kiểm tra dịch vụ:

12

WCF & Cloud

Windows Phone và WCF

private void btnGetData_Click(object sender,  RoutedEventArgs e)        { ServiceReference1.Service1Client client =             new ServiceReference1.Service1Client();             int data=int.Parse(txtData.Text); client.GetDataAsync(data); client.GetDataCompleted +=  client_GetDataCompleted; }  void client_GetDataCompleted(object sender,             ServiceReference1.GetDataCompletedEventArgs e)       { txtResult.Text = e.Result; }

13

WCF & Cloud

2. Windows Azure Mobile Services • Cách tạo một Mobile Service

• Làm việc với Data

• Làm việc với Server-Side Scripts: Validation và

Access Control

14

WCF & Cloud

Cách tạo một Mobile Service

15

WCF & Cloud

Làm việc với Data

Displayed As

Number

T­SQL Type in Azure  Table float(53) the highest­ precision data type

16

WCF & Cloud

JSON/CLR Data  Type Numeric values  (integer, decimal,  floating­point) Boolean DateTime String Bit DateTimeOffset nvarchar(max) Boolean Date String

Làm việc với Server-Side Scripts: Validation và Access Member Control OK CREATED ACCEPTED NO_CONTENT BAD_REQUEST UNAUTHORIZED FORBIDDEN NOT_FOUND CONFLICT INTERNAL_SERVER_ERROR

17

WCF & Cloud

Code 200 201 202 204 400 401 403 404 409 500

Thảo luận

18

WCF & Cloud