Khoa Công nghệ thông tin
Bộ môn HTTT
Chuyên đề Thương mại điện tử
Công nghệ Web services
1
Tháng 11-2006
Nội dung trình bày
Các ứng dụng phân tán
Kiến trúc hướng dịch vụ
Web service
Lập trình Web service
2 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Các ứng dụng phân tán Distributed Application
Dữ liệu
Máy tính
Ứng dụng phân tán
Dữ liệu
Máy tính
3 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Kiến trúc hướng dịch vụ Service-Oriented Architecture-SOA
Service Broker Service Broker
Publish
Find
Bind
Service Provider Service Provider
Service Consumer Service Consumer
4 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Web Service
Web Service là gì ?
Nền tảng của web service
Kiến trúc của web service
Mô hình của ứng dụng với Web service
5 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Web service là gì
Web Web Service Service
Web Web Service Service
Firewall
1
Là một tập các phương Là một tập các phương thức được gọi thực hiện thức được gọi thực hiện từ xa thông qua một địa từ xa thông qua một địa chỉ URL chỉ URL
Firewall
Intern Intern et et
2
Sử dụng để tạo các ứng Sử dụng để tạo các ứng dụng phân tán dụng phân tán
Firewall
Client Client
Web Web Service Service
6 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Đặc điểm
Không phụ thuộc vào ngôn ngữ lập trình Không phụ thuộc vào ngôn ngữ lập trình
1
Truy cập bất cứ ứng dụng nào Truy cập bất cứ ứng dụng nào
2
3
Hỗ trợ thao tác giữa các thành phần không đồng Hỗ trợ thao tác giữa các thành phần không đồng nhất nhất
Chi phí phát triển thấp Chi phí phát triển thấp
4
Dễ bảo trì Dễ bảo trì
5
7 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Kiến trúc của Web Service
UDDI (Web UDDI (Web Service Service Broker) Broker)
Publish
Find
Interne Interne t t
Bind
Web Service Web Service Provider Provider
Web Service Web Service Consumer Consumer
8 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Kiến trúc của Web Service
UDD UDD I I
SOAP
SOAP
IISIIS
SOAP
Web Web Service Service
Any Any Client Client
9 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Web service provider
Web Server Web Server
1
Cung cấp Web service Cung cấp Web service
2
IISIIS
Yêu cầu Yêu cầu
Hỗ trợ 1 protocol listenner Hỗ trợ 1 protocol listenner
1
Có các cơ chế bảo mật Có các cơ chế bảo mật
2
3
Cung cấp đúng service mà Cung cấp đúng service mà consumer yêu cầu consumer yêu cầu
10 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Web service consumer
Sử dụng Web service do Sử dụng Web service do WS Provider cung cấp để WS Provider cung cấp để xây dựng ứng dụng xây dựng ứng dụng
11 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Web service broker
UDDI UDDI
Là môi giới giữa consumer và provider Là môi giới giữa consumer và provider
1
Tương tác với provider : để lấy các thông tin về web service Tương tác với provider : để lấy các thông tin về web service
2
3
Tương tác với consumer :cung cấp cho consumer địa chỉ Tương tác với consumer :cung cấp cho consumer địa chỉ các Web service các Web service
4
Sử dụng UDDI registries (Universal Description, Discovery Sử dụng UDDI registries (Universal Description, Discovery and Integration) and Integration)
12 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Mô hình tương tác giữa các thành phần
5
1
Pricing Web Service Pricing Web Service
3
UDDI Services UDDI Services
4
2
Web service developer builds and deploys a pricing Web service Web service developer builds and deploys a pricing Web service
1
Web service developer registers and categorizes the Web service Web service developer registers and categorizes the Web service
2
Web service consumer queries UDDI for “pricing” services Web service consumer queries UDDI for “pricing” services
3
4
Web service consumer determines the most appropriate “pricing” Web service consumer determines the most appropriate “pricing” service service
5
Web service developer builds a solution that directly Web service developer builds a solution that directly consumes the Web service data consumes the Web service data
13 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Nền tảng của Web Service
Web Service
HTTP SOAP XML
14 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
HTTP – Hyper text transfer protocol
HTTP Request HTTP Request
POST /TheStockExchange/Trading/GetStockPrice.asp HTTP/1.1 POST /TheStockExchange/Trading/GetStockPrice.asp HTTP/1.1 Host: localhost Host: localhost ContentType: application/xwwwformurlencoded ContentType: application/xwwwformurlencoded ContentLength: 11 ContentLength: 11
Symbol=MSFT Symbol=MSFT
HTTP/1.1 200 OK HTTP/1.1 200 OK ContentType: text/xml; charset=utf8 ContentType: text/xml; charset=utf8 ContentLength: 75 ContentLength: 75
HTTP Response HTTP Response
15 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
XML – Extend Markup Language
Sử dụng để mô tả Web service interface Sử dụng để mô tả Web service interface
1
SOAP Message là một tài liệu XML SOAP Message là một tài liệu XML
2
3
Sử dụng để serialize các đối tượng thành một tài liệu Sử dụng để serialize các đối tượng thành một tài liệu XML (System.Xml.Serialization) XML (System.Xml.Serialization)
16 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
SOAP(1)
SOAP – Simple Object Access Protocol SOAP – Simple Object Access Protocol
1
Protocol for message-based communication Protocol for message-based communication
2
XML-Based protocol XML-Based protocol
3
An envelope for handling extensibility and An envelope for handling extensibility and modularity modularity
An encoding mechanism for representing An encoding mechanism for representing types within an envelope types within an envelope
SOAP ≈ HTTP + XML SOAP ≈ HTTP + XML
4
Submitted to W3C Submitted to W3C
5
17 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
SOAP (2)
SOAP Request : HTTP POST Request SOAP Request : HTTP POST Request
6
POST /WebCalculator/Calculator.asmx HTTP/1.1 Content-Type: text/xml SOAPAction: “http://tempuri.org/Add” Content-Length: 386
...
18 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
SOAP(3) - Message Structure
The complete SOAP message
SOAP Message
Protocol binding headers
Headers
encloses payload
SOAP Envelope
encloses headers
SOAP Header
Individual headers
Headers
contains SOAP message name
SOAP Body
XML-encoded SOAP message name
Message Name & Data
& data
19 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
SOAP(4) - Example of a SOAP Request
POST /StockQuote HTTP/1.1 Host: www.stockquoteserver.com ContentType: text/xml; charset="utf8" ContentLength: nnnn SOAPAction: "SomeURI“
DIS
20 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
SOAP(5) - Example of a SOAP Response
HTTP/1.1 200 OK ContentType: text/xml; charset="utf8" ContentLength: nnnn
34.5
21 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Lập trình Web service bằng VS.NET 2003
Web service programming - provider side Web service programming - provider side
1
Web service programming - consumer side Web service programming - consumer side
2
22 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Lập trình Web service (1)
Loại Project : ASP.NET Web Service Loại Project : ASP.NET Web Service
1
Các name space thường dùng Các name space thường dùng
2
System System System.Data System.Data System.Web System.Web System.Web.Webservices System.Web.Webservices System.Xml System.Xml
Các thành phần Các thành phần
3
File *.asmx File *.asmx File global.asax File global.asax File web.config File web.config Thư mục Bin Thư mục Bin
23 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Lập trình Web service (2)
Khai báo Web service method Khai báo Web service method
4
Thêm chỉ thị [Web Method] Thêm chỉ thị [Web Method]
Ví dụVí dụ
[WebMethod] [WebMethod] public int Sum(int a, int b) public int Sum(int a, int b) { {
return a + b; return a + b;
} }
24 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Lập trình Web service (3)
Các thuộc tính của [WebMethod] Các thuộc tính của [WebMethod]
5
BufferResponse BufferResponse
CacheDuration CacheDuration
Description Description
EnableSession EnableSession
TransactionOption TransactionOption
25 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Lập trình Web service (3)
Ví dụVí dụ
[WebMethod(BufferResponse=false)] [WebMethod(BufferResponse=false)] public Transactions public Transactions GetTransactionHistory() GetTransactionHistory() { {
//implementation code //implementation code
} }
[WebMethod (Description=“This method using for [WebMethod (Description=“This method using for converting ..” )] converting ..” )] public double ConvertTemperature(double dFahrenheit) public double ConvertTemperature(double dFahrenheit) { {
return ((dFahrenheit - 32) * 5) / 9; return ((dFahrenheit - 32) * 5) / 9;
} }
26 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Lập trình Web service (4)
XML Serialization XML Serialization
5
Dùng để xác định cấu trúc tài liệu XML mong Dùng để xác định cấu trúc tài liệu XML mong muốn để biểu diễn đối tượng của một lớp muốn để biểu diễn đối tượng của một lớp
Namespace : System.Xml.Serialization Namespace : System.Xml.Serialization
XmlArray XmlArray
XmlRoot XmlRoot
XmlArrayItem XmlArrayItem
XmlElement XmlElement
XmlAttribute XmlAttribute
27 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Lập trình Web service (5)
[XmlRoot("account")] [XmlRoot("account")] public class Acct public class Acct Ví dụ 1 Ví dụ 1 { {
[XmlElement("description")] [XmlElement("description")] public string public string
Description; Description;
[XmlElement("number")] [XmlElement("number")] public string public string
HelloHello
ption>
10
10
C
C
Number; Number;
[XmlElement("type")] [XmlElement("type")] public string Type; public string Type;
[XmlElement("balance")] [XmlElement("balance")]
public decimal public decimal
Balance; Balance;
[XmlAttribute("status")] [XmlAttribute("status")]
28
public string Status; public string Status; 11/2006
} }
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Lập trình Web service (6)
… Ví dụ 2 … Ví dụ 2 [return:XmlArray("AccountLi [return:XmlArray("AccountLi
st")] st")]
[return:XmlArrayItem("Acco [return:XmlArrayItem("Acco
unt")] unt")] public Acct[] public Acct[]
GetAllAccounts() GetAllAccounts()
… …
………
………
….
….
29 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Lập trình Web service (7)
Ví dụ 3 Ví dụ 3
… … [return:XmlArrayItem(ElementName="savingsAcct", Type=typeof(SavingsAcct))] [return:XmlArrayItem(ElementName="savingsAcct", Type=typeof(SavingsAcct))] [return:XmlArrayItem(ElementName="creditCardAcct", Type=typeof(CreditCardAcct))] [return:XmlArrayItem(ElementName="creditCardAcct", Type=typeof(CreditCardAcct))] [WebMethod] [WebMethod] public Acct[] GetAllAccounts() public Acct[] GetAllAccounts() { { ... ...
…
…
…
…
…
…
30 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Lập trình Web service (8)
Kiểu dữ liệu trong web service method Kiểu dữ liệu trong web service method
6
Simple data types Simple data types
Input and output parameter (in, ref,…) Input and output parameter (in, ref,…)
Variable length parameter list (homogeneous, Variable length parameter list (homogeneous, heterogeneous ) heterogeneous )
Complex data type Complex data type
Classes and structures Classes and structures
Array, Collections Array, Collections
DataSets DataSets
31 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Lập trình Web service – provider side(9)
Trường hợp lưu ý Trường hợp lưu ý
Nếu tham số web service method là 1 mảng các đối Nếu tham số web service method là 1 mảng các đối tượng có kiểu không rõ ràng tượng có kiểu không rõ ràng
Phải khai báo tường minh tất cả các kiểu dữ liệu Phải khai báo tường minh tất cả các kiểu dữ liệu có thể có trong mảng có thể có trong mảng
Sử dụng :XmlInclude Sử dụng :XmlInclude Name space System.Xml.Serialization Name space System.Xml.Serialization
32 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Lập trình Web service – provider side(10)
Ví dụVí dụ
[WebMethod] [WebMethod] [XmlInclude(typeof(CreditCardAcct))] [XmlInclude(typeof(CreditCardAcct))] [XmlInclude(typeof(SavingsAcct))] [XmlInclude(typeof(SavingsAcct))] [return:XmlArray("AccountList")] [return:XmlArray("AccountList")] [return:XmlArrayItem("Account")] [return:XmlArrayItem("Account")] public Acct[] GetAllAccounts() public Acct[] GetAllAccounts() { { SavingsAcct a = new SavingsAcct(); SavingsAcct a = new SavingsAcct(); CreditCardAcct cc = new CreditCardAcct cc = new CreditCardAcct(); CreditCardAcct(); // populate the accounts // populate the accounts Acct [] sa = new Acct[2]; Acct [] sa = new Acct[2]; sa[0] = a; sa[0] = a; sa[1] = cc; sa[1] = cc; return sa; return sa; } }
public class Acct public class Acct { { public string Description; public string Description; public string Number; public string Number; public string Type; public string Type; public decimal Balance; public decimal Balance; public string Status; public string Status; } } public class SavingsAcct : Acct public class SavingsAcct : Acct { { public decimal MinimumBalance; public decimal MinimumBalance; } } public class CreditCardAcct : Acct public class CreditCardAcct : Acct { { public int PayPeriod; public int PayPeriod; } }
33 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Triển khai Web service
Web Service Files
Needed
Remov e
1
Tự chép các file cần Tự chép các file cần thiết lên Web server thiết lên Web server
.sln, .vbproj, .csproj, .vsdisco, .webinfo
.resx
.vb, .cs
.xsd
2
\Web References folder and files
Sử dụng Windows Sử dụng Windows Installer Files Installer Files Tạo 1 web setup Tạo 1 web setup project trong VS.NET project trong VS.NET
\bin directory and .dll(s)
.asmx
Web.config
Global.asax
.xml
34 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Web Service Consumer
WSDL Documents WSDL Documents
1
Proxy class Proxy class
2
Consuming Web service in VS.NET 2003 Consuming Web service in VS.NET 2003
3
35 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
WSDL Documents
1
Web Service Description Web Service Description Language Language
2
XML schema : Mô tả giao diện của XML schema : Mô tả giao diện của web service web service
Sử dụng đề phát sinh proxy class Sử dụng đề phát sinh proxy class
3
4
Xem WSDL Document của 1 web service Xem WSDL Document của 1 web service http://localhost/TestService/service.asmx?wsdl http://localhost/TestService/service.asmx?wsdl
36 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Proxy class
Proxy : Ủy nhiệm Proxy : Ủy nhiệm
1
Được phát sinh từ WSDL Document Được phát sinh từ WSDL Document
2
Được sử đụng đề giao tiếp với Web service Được sử đụng đề giao tiếp với Web service
3
37 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Sử dụng Web service trong .NET
Tạo lớp proxy để giao tiếp với web service Tạo lớp proxy để giao tiếp với web service
1
Sử dụng wsdl.exe Sử dụng wsdl.exe
wsdl [options] {URL | Path} wsdl [options] {URL | Path}
wsdl http://www.woodgrovebank.com/services/bank.asmx?wsdl wsdl http://www.woodgrovebank.com/services/bank.asmx?wsdl
Sử dụng VS.Net Sử dụng VS.Net
Add Web References … Add Web References …
Sử dụng lớp proxy để gọi các phương thức của web service Sử dụng lớp proxy để gọi các phương thức của web service
2
Gọi bình thường như các phương thức khác Gọi bình thường như các phương thức khác
38 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
WSDL Document
39 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Proxy Class
40 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
SOAP Request and Response
41 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
HTTP Request and Response
42 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Web service and State Management
ASP.NET Web service Application Web Applicaton ASP.NET Web service Application Web Applicaton
1
Web service Application : Stateless Web service Application : Stateless
2
Sử dụng đối tượng Application và Session đề quản lý Sử dụng đối tượng Application và Session đề quản lý trạng thái của web service trạng thái của web service
43 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Ứng dụng của Web Services
1
WS là một tập hợp các chức năng được thực thi qua môi WS là một tập hợp các chức năng được thực thi qua môi trường mạng, được các ứng dụng khác sử dụng trường mạng, được các ứng dụng khác sử dụng
2
Một WS có thể kết hợp với các WS khác để đưa ra tính năng Một WS có thể kết hợp với các WS khác để đưa ra tính năng cao hơn cao hơn
3
Mô hình ứng dụng trong tương lai sẽ là sự kết hợp giữa các Mô hình ứng dụng trong tương lai sẽ là sự kết hợp giữa các service service
……4
44 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Demo
45 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT
Hỏi và đáp
46 11/2006
ộ Môn: Chuyên đ ề TMĐT – B môn
HTTT