Thi t k & L p trình Web 2
Bài 10
Web Services
Ths Tr n Th Bích H nh
Khoa CNTT
© 2009 Khoa Công ngh thông tin
N i
N i dungdung
N i
N i dungdung
Gi i thi u Web Services
Ki n trúc Web Services
L p trình Web Services b ng VS.NET
© 2009 Khoa CNTT - HKHTN
2
Thi t k & L p trình Web 2 – Bài 10: Web Services
N i
N i dungdung
N i
N i dungdung
Gi i thi u Web Services
Ki n trúc Web Services
L p trình Web Services b ng VS.NET
Thi t k & L p trình Web 2 – Bài 10: Web Services
Làm Làm saosao thanhthanh toántoán hóahóađ nđ n đi nđi n tho itho i online?online? Làm
Làm saosao thanhthanh toántoán hóahóađ nđ n đi nđi n tho itho i online?online?
1
Internet
2 Pay Bill
P Bill L thô ti Hó đ t T l
Bank
Pay Bill L y thông tin Hóa đ n t Telecom
1 Pay Bill Yêu c u Bank chuy n kho n t Customer Account sang Telecom Account
2 Pay Bill Thông báo cho Telecom bi t c p nh t tình tr ng hóa đ n đã thanh toán
3
Trang 2Web Service
1 Internet
Telecom Ser ice
1
3 Telecom Service
2
3 Pay Bill
Bank Service 2
© 2009 Khoa CNTT - HKHTN
5
cđi mđi m Web ServicesWeb Services
cđi mđi m Web ServicesWeb Services
T mô t
D a trên n n t ng XML
Chi phí phát tri n th p
D b o trì
© 2009 Khoa CNTT - HKHTN
6
Thi t k & L p trình Web 2 – Bài 10: Web Services
N i
N i dungdung
N i
N i dungdung
Gi i thi u Web Services
Ki n trúc Web Services
L p trình Web Services b ng VS.NET
Thi t k & L p trình Web 2 – Bài 10: Web Services
Ki n
Ki n trúctrúc Web ServicesWeb Services
Ki n
Ki n trúctrúc Web ServicesWeb Services
UDDI (Web Service Broker)
Internet
Bind
Web Service
Consumer Consumer
Trang 3Ki n
Ki n trúctrúc c ac a Web ServicesWeb Services
Ki n
Ki n trúctrúc c ac a Web ServicesWeb Services
UDDI
WSDL
IIS
Any Client
SOAP
Web Service
© 2009 Khoa CNTT - HKHTN
9
y
Standards
• SOAP (Simple Object Access Protocol)
Giao th c trao đ i thông tin c a service theo đ nh d ng XML
– Giao th c trao đ i thông tin c a service theo đ nh d ng XML
– Ngôn ng đ c t web service
• UDDI (Universal Description Discovery and Integration) – Th Th m c l u tr m c l u tr thông tin v Web services thông tin v Web services
– Cho phép các công ty đ ng ký & tìm ki m web services
© 2009 Khoa CNTT - HKHTN
10
Thi t k & L p trình Web 2 – Bài 10: Web Services
SOAP
– Cho phép truy n các thông đi p (message) qua giao th c
HTTP
– S d ng XML đ mô t d li u
– S d ng XML đ mô t d li u
• Requestor gói thông tin yêu c u vào m t message và g i q g g y g g
lên web service
• Web Service phân tích message g i lên và th c thi hàm x
lý yêu c u ý y
• K t qu đ c gói vào m t message và g i v cho
Requestor
• Requestor phân tích message tr v và hi n th k t qu Requestor phân tích message tr v và hi n th k t qu
– c t : http://www.w3.org/TR/soap
Thi t k & L p trình Web 2 – Bài 10: Web Services
Ví
Ví
Trang 4– c t : http://www.w3.org/TR/wsdl
© 2009 Khoa CNTT - HKHTN
13
WSDL Example
© 2009 Khoa CNTT - HKHTN
14
Thi t k & L p trình Web 2 – Bài 10: Web Services
UDDI
• Universal Description Discovery and Integration
– Th m c l u tr thông tin v web services
– Cho phép doanh nghi p đ ng ký service c a mình vào
danh sách các services và tìm ki m service c a nh ng
doanh nghi p khác đ s d ng
– c t b ng WSDL
– Giao ti p thông qua SOAP
– c xây d ng trong Microsoft NET platform
– c t : http://www.uddi.org/pubs/uddi_v3.htm
Thi t k & L p trình Web 2 – Bài 10: Web Services
N i
N i dungdung
N i
N i dungdung
Gi i thi u Web Services
Ki n trúc Web Services
L p trình Web Services b ng VS.NET
Trang 5Mô hình t ng tác gi a các thành ph n
1
5 1
Pricing Web Service
4 2
Web service developer xây d ng & tri n khai “pricing” Web service
1
4
Web service developer xây d ng & tri n khai pricing Web service
1
Web service developer đ ng ký & phân lo i Web service
2
Web service consumer truy xu t UDDI đ tìm “pricing” services
3
Web service consumer ch n “pricing” service phù h p nh t
4
© 2009 Khoa CNTT - HKHTN
17
Web service consumer g i & nh n k t qu t web service
5
L p trình Web service b ng VS NET
Web service programming - provider side
11
Web Service Provider Response
Request
Web Service Consumer Request
© 2009 Khoa CNTT - HKHTN
18
Web service programming - consumer side
22
Thi t k & L p trình Web 2 – Bài 10: Web Services
Web Service Provider Side
Lo i Project – New Website
Web Service Provider – ASP.NET Web Service ho c
– ASP.NET Web Application
Thi t k & L p trình Web 2 – Bài 10: Web Services
Web Service Class
// Service.asmx
<%@ WebService Language="C#" CodeBehind="~/App_Code/Service.cs"
Cl " i " %>
// Service.asmx
<%@ WebService Language="C#" CodeBehind="~/App_Code/Service.cs"
Cl " i " %>
Class=" Service " %>
Class=" Service " %>
// Service.cs
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Service : System.Web.Services.WebService
{ bli S i () { }
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Service : System.Web.Services.WebService
{ bli S i () { } public Service () { }
[WebMethod]
public string HelloWorld() { return "Hello World";
public Service () { }
[WebMethod]
public string HelloWorld() { return "Hello World";
} } } }
Trang 6Khai báobáo phph ngng th cth c Web ServiceWeb Service
Khai
Khai báobáo phph ngng th cth c Web ServiceWeb Service
B sung ch th [WebMethod] tr c khai báo hàm
Cá th tí h [W bM th d]
Các thu c tính c a [WebMethod]
– BufferResponse
– CacheDuration
– Description
– EnableSession
T ti O ti
[ WebMethod ( BufferResponse=false )]
public Transactions GetTransactionHistory()
[ WebMethod ( BufferResponse=false )]
public Transactions GetTransactionHistory()
– TransactionOption pub c{ a sact o s Get a sact o sto y()
//implementation code }
pub c a sact o s Get a sact o sto y() {
//implementation code }
[ WebMethod ( Description=“This method using for converting ” )]
public double ConvertTemperature(double dFahrenheit)
{
[ WebMethod ( Description=“This method using for converting ” )]
public double ConvertTemperature(double dFahrenheit)
{
© 2009 Khoa CNTT - HKHTN
21
return ((dFahrenheit - 32) * 5) / 9;
}
return ((dFahrenheit - 32) * 5) / 9;
}
XML Serialization
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
bi u di n đ i t ng c a m t l p
XmlArray Namespace : System.Xml.Serialization
XmlElement
XmlRoot
XmlArrayItem XmlArray
© 2009 Khoa CNTT - HKHTN
22
Thi t k & L p trình Web 2 – Bài 10: Web Services
Ví
Ví dd 11 –– XmlRootXmlRoot && XmlElementXmlElement
Ví
Ví dd 1 1 –– XmlRootXmlRoot & & XmlElementXmlElement
<account status=“123”>
[ XmlRoot ("account")]
public class Acct
{
[ XmlElement ("description")]
<description>Hello</description>
<number>10 </number>
<type>C</type>
<account>
[ e e ( desc p o )]
public string Description;
[ XmlElement ("number")]
public string Number;
[ XmlElement ("type")]
public string Type;
[ XmlElement ("balance")]
public decimal Balance;
[ XmlAttribute ("status")]
public string Status;
}
Thi t k & L p trình Web 2 – Bài 10: Web Services
Ví
Ví dd 22 –– XmlArrayXmlArray && XmlArrayItemXmlArrayItem Ví
Ví dd 2 2 –– XmlArrayXmlArray & & XmlArrayItemXmlArrayItem
… [return: XmlArray (" AccountList ")]
[return: XmlArrayItem (" Account ")] <AccountList>
<account>
public Acct[] GetAllAccounts()
…
<account>
………
<account>
<account>
….
</account>
<AccountList>
Trang 7Ví dd 33 –– XmlArrayItemXmlArrayItem && ElementNameElementName
Ví
Ví dd 3 3 –– XmlArrayItemXmlArrayItem & & ElementNameElementName
…
[return: XmlArrayItem (ElementName="savingsAcct", Type=typeof(SavingsAcct))]
[return: XmlArrayItem (ElementName="creditCardAcct", Type=typeof(CreditCardAcct))]
[WebMethod]
public Acct[] GetAllAccounts() { … }
A tLi t
<AccountList>
…
<SavingAcct>
</SavingActt>
…
<CreditCardAcct>
</CreditCardActt>
… /A tLi t
© 2009 Khoa CNTT - HKHTN
25
</AccountList>
Ví
Ví dd 44 XmlIncludeXmlInclude Ví
Ví dd 4 4 XmlIncludeXmlInclude
public class Acct
{ public string Description;
public string Number;
public string Type;
[ XmlInclude ( typeof(CreditCardAcct) )] [ XmlInclude ( typeof(SavingsAcct) )] [return: XmlArray ("AccountList")] [return: XmlArrayItem ("Account")]
public decimal Balance;
public string Status;
} bli l S i A t A t
[return: XmlArrayItem ( Account )] public Acct [] GetAllAccounts () {
SavingsAcct a = new SavingsAcct();
C ditC dA t public class SavingsAcct : Acct
{ public decimal MinimumBalance;
}
CreditCardAcct cc = new CreditCardAcct();
// populate the accounts Acct [] sa = new Acct[2];
} public class CreditCardAcct : Acct {
public int PayPeriod;
cc [] sa e cc [ ];
sa[0] = a;
sa[1] = cc;
return sa;
}
© 2009 Khoa CNTT - HKHTN
26
Thi t k & L p trình Web 2 – Bài 10: Web Services
Tri n
Tri n khaikhai Web ServiceWeb Service
Tri n
Tri n khaikhai Web ServiceWeb Service
Public Website cho Web Service Application
Web Service Precomplied Directory
\bin directory and *.dll(s)
* asmx asmx Web.config Global.asax
T o Virtual Directory cho “Web Servicce Precomplied Directory” nh
*.xml
tri n khai m t ng d ng Web
Xem WSDL Document c a 1 web service
http://localhost/WebService/service asmx?wsdl
http://localhost/WebService/service.asmx?wsdl
Thi t k & L p trình Web 2 – Bài 10: Web Services
Web Service Consumer Side
Lo i Project – ASP.NET Web Service
– ASP.NET Web Application
Wi d A li i
– Window Application
– …
Web Service Consumer
G i Web Service – S d ng Proxy Class
Web Service Consumer
Trang 8Application Model
Web Service
Developer
Web Application Developer
Web Server 1
Service Provider App
asmx
.aspx
Web Server 2
Proxy Web Form
© 2009 Khoa CNTT - HKHTN
Service Consumer App
29
Proxy class
L p giao ti p v i Web Service
© 2009 Khoa CNTT - HKHTN
30
Thi t k & L p trình Web 2 – Bài 10: Web Services
Add Web Reference
Thi t k & L p trình Web 2 – Bài 10: Web Services
G i
G i th cth c thithi Web ServiceWeb Service
G i
G i th cth c thithi Web ServiceWeb Service
// Default.aspx.cs WebService Service service = new WebService Service(); (); string str = service.HelloWorld();
Trang 9Web service
Web service vàvà State ManagementState Management
Web service
Web service vàvà State ManagementState Management
ASP NET Web service Application Web Applicaton
1 ASP.NET Web service Application Web Applicaton
1
Web service Application : Stateless
2
S d ng đ i t ng Application và Session đ qu n lý tr ng thái c a web
service
© 2009 Khoa CNTT - HKHTN
33
T ng
T ng
Web Services
Ki n trúc Web Services – Web Service Provider
– Web Service Consumer
– Web Service Broker
Web Service Standards – SOAP
– UDDI
© 2009 Khoa CNTT - HKHTN
L p trình Web Services b ng VS.NET
34