SlideShare a Scribd company logo
Chuyên đề Thương mại điện tử Công nghệ Web services   Tháng 11-2006 Khoa Công nghệ thông tin Bộ môn HTTT
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
Các ứng dụng phân tán Distributed Application Dữ liệu Ứng dụng phân tán Dữ liệu Máy tính Máy tính
Kiến trúc hướng dịch vụ Service-Oriented Architecture-SOA Publish Find Bind Service Consumer Service Provider Service Broker
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
Web service là gì Internet Firewall Firewall Firewall Web Service Web Service Web Service Client Là một tập các phương thức được gọi thực hiện từ xa thông qua một địa chỉ URL 1 Sử dụng để tạo các ứng dụng phân tán 2
Đặc điểm 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 2 Hỗ trợ thao tác giữa các thành phần không đồng nhất 3 Chi phí phát triển thấp 4 Dễ bảo trì 5
Kiến trúc của Web Service Publish Find Bind Internet Web Service Provider UDDI (Web Service Broker) Web Service Consumer
Kiến trúc của Web Service Any Client SOAP SOAP SOAP UDDI IIS Web Service
Web service provider IIS Yêu cầu Hỗ trợ 1 protocol listenner 1 Có các cơ chế bảo mật 2 Cung cấp đúng service mà consumer yêu cầu 3 Web Server 1 Cung cấp Web service 2
Web service consumer Sử dụng Web service do WS Provider cung cấp để xây dựng ứng dụng
Web service broker UDDI 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 2 Tương tác với consumer : cung cấp cho consumer địa chỉ các Web service 3 Sử dụng UDDI registries (Universal Description, Discovery and Integration) 4
Mô hình tương tác giữa các thành phần 4 2 1 5 Web service developer  builds and deploys a pricing Web service 1 Web service developer  registers and  categorizes the Web service 2 Web service consumer  queries UDDI for “pricing” services 3 Web service consumer  determines the most appropriate “pricing” service 4 Web service developer  builds a solution that directly  consumes the Web service data 5 Pricing Web Service UDDI Services 3
Nền tảng của Web Service Web Service XML SOAP HTTP
HTTP  – Hyper text transfer protocol POST   /TheStockExchange/Trading/GetStockPrice.asp  HTTP/1.1 Host: localhost Content-Type: application/x-www-form-urlencoded Content-Length: 11 Symbol=MSFT HTTP Request HTTP/1.1  200  OK Content-Type: text/xml; charset=utf-8 Content-Length: 75 <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> <stock symbol=&quot;MSFT&quot; Price=&quot;71.50&quot; /> HTTP Response
XML – Extend Markup Language Sử dụng để mô tả Web service interface 1 SOAP Message là một tài liệu XML 2 Sử dụng để serialize các đối tượng thành một tài liệu XML (System.Xml.Serialization) 3
SOAP(1) An envelope for handling extensibility and modularity   An encoding mechanism for representing types within an envelope  XML-Based protocol 3 Submitted to W3C 5 Protocol for message-based communication 2 SOAP ≈ HTTP + XML 4 SOAP  – Simple Object Access Protocol 1
SOAP (2) POST /WebCalculator/Calculator.asmx HTTP/1.1 Content-Type: text/xml SOAPAction: “http://tempuri.org/Add” Content-Length: 386 <?xml version=“1.0”?> <soap:Envelope ...> ... </soap:Envelope> SOAP Request : HTTP POST Request 6
SOAP(3) -  Message Structure SOAP Message SOAP Envelope SOAP Header SOAP Body Message Name & Data Headers Headers XML-encoded SOAP message name  & data <Body>  contains SOAP message name Individual headers <Header>  encloses headers <Envelope>  encloses payload Protocol binding headers The complete SOAP message
SOAP(4) -  Example of a SOAP Request POST /StockQuote HTTP/1.1 Host: www.stockquoteserver.com Content-Type: text/xml;  charset=&quot;utf-8&quot; Content-Length: nnnn SOAPAction: &quot;Some-URI“ <SOAP-ENV:Envelope xmlns:SOAP-ENV=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot; SOAP-ENV: encodingStyle = &quot;http://schemas.xmlsoap.org/soap/encoding/&quot;> <SOAP-ENV:Body> <m:GetLastTradePrice xmlns:m=&quot;Some-URI&quot;> <symbol>DIS</symbol> </m:GetLastTradePrice> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
SOAP(5) -  Example of a SOAP Response HTTP/1.1 200 OK Content-Type: text/xml;  charset=&quot;utf-8&quot; Content-Length: nnnn <SOAP-ENV:Envelope xmlns:SOAP-ENV= &quot;http://schemas.xmlsoap.org/soap/envelope/&quot; SOAP-ENV: encodingStyle= &quot;http://schemas.xmlsoap.org/soap/encoding/&quot;/> <SOAP-ENV:Body> <m:GetLastTradePriceResponse xmlns:m=&quot;Some-URI&quot;> <Price>34.5</Price> </m:GetLastTradePriceResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
Lập trình Web service bằng VS.NET 2003 Web service programming - provider side 1 Web service programming - consumer side 2
Lập trình Web service (1)  System System.Data System.Web System.Web.Webservices System.Xml File *.asmx File global.asax File web.config Thư mục Bin Loại Project : ASP.NET Web Service 1 Các name space thường dùng 2 Các thành phần 3
Lập trình Web service (2) Thêm chỉ thị  [Web Method] Khai báo Web service method 4 ,[object Object],[object Object],[object Object],[object Object],[object Object],Ví dụ
Lập trình Web service (3) BufferResponse CacheDuration Description EnableSession TransactionOption Các thuộc tính của  [WebMethod] 5
Lập trình Web service (3) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Ví dụ
Lập trình Web service (4) 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 Namespace :  System.Xml.Serialization XmlElement XmlAttribute XmlRoot XML Serialization 5 XmlArrayItem XmlArray
Lập trình Web service (5) [ XmlRoot (&quot;account&quot;)] public class Acct { [ XmlElement (&quot;description&quot;)]  public string Description; [ XmlElement (&quot;number&quot;)]  public string Number; [ XmlElement (&quot;type&quot;)]  public string Type; [ XmlElement (&quot;balance&quot;)]  public decimal Balance; [ XmlAttribute (&quot;status&quot;)]  public string Status; } Ví dụ 1 <account status=“123”> <description>Hello</description> <number>10  </number> <type>C</type> <account>
Lập trình Web service (6) … [return: XmlArray (&quot; AccountList &quot;)] [return: XmlArrayItem (&quot; Account &quot;)] public  Acct[]  GetAllAccounts() … <AccountList> <account> ……… <account> <account> … . </account> <AccountList> Ví dụ 2
Lập trình Web service (7) … [return: XmlArrayItem (ElementName=&quot;savingsAcct&quot;, Type=typeof(SavingsAcct))] [return: XmlArrayItem (ElementName=&quot;creditCardAcct&quot;, Type=typeof(CreditCardAcct))] [WebMethod] public Acct[] GetAllAccounts() { ... <AccountList> … <SavingAcct> </SavingActt> … <CreditCardAcct> </CreditCardActt> … </AccountList> Ví dụ 3
Lập trình Web service (8) Input and output parameter (in, ref,…) Variable length parameter list ( homogeneous, heterogeneous   ) Simple data types Kiểu dữ liệu trong web service method 6 Classes and structures Array, Collections DataSets Complex data type
Lập trình Web service – provider side(9)    Phải khai báo tường minh tất cả các kiểu dữ liệu có thể có trong mảng ,[object Object],[object Object],Trường hợp lưu ý Nếu tham số web service method là 1 mảng các đối tượng có kiểu không rõ ràng
Lập trình Web service – provider side(10) Ví dụ public class  Acct { public string Description; public string Number; public string Type; public decimal Balance; public string Status; } public class  SavingsAcct  : Acct { public decimal MinimumBalance; } public class  CreditCardAcct  : Acct { public int PayPeriod; } [ WebMethod ] [ XmlInclude (typeof(CreditCardAcct))] [ XmlInclude (typeof(SavingsAcct))] [return: XmlArray (&quot;AccountList&quot;)] [return: XmlArrayItem (&quot;Account&quot;)] public  Acct []  GetAllAccounts () { SavingsAcct a = new SavingsAcct(); CreditCardAcct cc = new CreditCardAcct(); // populate the accounts Acct [] sa = new Acct[2]; sa[0] = a; sa[1] = cc; return sa; }
Triển khai Web service eb References folder and files .xsd Web.config in directory and .dll(s) .xml Global.asax .asmx .vb, .cs Needed Remove Web Service Files .sln, .vbproj, .csproj, .vsdisco, .webinfo .resx Tự chép các file cần thiết lên Web server 1 Sử dụng Windows Installer Files Tạo 1 web setup project trong VS.NET 2
Web Service Consumer WSDL Documents 1 Proxy class 2 Consuming Web service  in VS.NET 2003  3
WSDL Documents XML schema : Mô tả giao diện của web service 2 Web Service Description Language 1 Sử dụng đề phát sinh proxy class 3 Xem WSDL Document của 1 web service http://localhost/TestService/service.asmx?wsdl 4
Proxy class Được phát sinh từ WSDL Document 2 Được sử đụng đề giao tiếp với Web service 3 Proxy  : Ủy nhiệm 1
Sử dụng Web service trong .NET Sử dụng wsdl.exe Sử dụng VS.Net Gọi bình thường như các phương thức khác Add Web References … wsdl [ options ] { URL  |  Path } wsdl   http://www.woodgrovebank.com/services/bank.asmx?wsdl Tạo lớp proxy để giao tiếp với web service 1 Sử dụng lớp proxy để gọi các phương thức của web service 2
WSDL Document
Proxy Class
SOAP Request and Response
HTTP Request and Response
Web service and State Management Sử dụng  đối tượng  Application  và  Session  đề quản lý trạng thái của web service ASP.NET Web service Application    Web Applicaton 1 Web service Application : Stateless 2
Ứng dụng của Web Services 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 1 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 2 Mô hình ứng dụng trong tương lai sẽ là sự kết hợp giữa các service 3 … 4
Demo
Hỏi và đáp

More Related Content

Viewers also liked

What Makes Great Infographics
What Makes Great InfographicsWhat Makes Great Infographics
What Makes Great Infographics
SlideShare
 
Masters of SlideShare
Masters of SlideShareMasters of SlideShare
Masters of SlideShare
Kapost
 
STOP! VIEW THIS! 10-Step Checklist When Uploading to Slideshare
STOP! VIEW THIS! 10-Step Checklist When Uploading to SlideshareSTOP! VIEW THIS! 10-Step Checklist When Uploading to Slideshare
STOP! VIEW THIS! 10-Step Checklist When Uploading to Slideshare
Empowered Presentations
 
You Suck At PowerPoint!
You Suck At PowerPoint!You Suck At PowerPoint!
You Suck At PowerPoint!
Jesse Desjardins - @jessedee
 
10 Ways to Win at SlideShare SEO & Presentation Optimization
10 Ways to Win at SlideShare SEO & Presentation Optimization10 Ways to Win at SlideShare SEO & Presentation Optimization
10 Ways to Win at SlideShare SEO & Presentation Optimization
Oneupweb
 
How To Get More From SlideShare - Super-Simple Tips For Content Marketing
How To Get More From SlideShare - Super-Simple Tips For Content MarketingHow To Get More From SlideShare - Super-Simple Tips For Content Marketing
How To Get More From SlideShare - Super-Simple Tips For Content Marketing
Content Marketing Institute
 
A Guide to SlideShare Analytics - Excerpts from Hubspot's Step by Step Guide ...
A Guide to SlideShare Analytics - Excerpts from Hubspot's Step by Step Guide ...A Guide to SlideShare Analytics - Excerpts from Hubspot's Step by Step Guide ...
A Guide to SlideShare Analytics - Excerpts from Hubspot's Step by Step Guide ...SlideShare
 
2015 Upload Campaigns Calendar - SlideShare
2015 Upload Campaigns Calendar - SlideShare2015 Upload Campaigns Calendar - SlideShare
2015 Upload Campaigns Calendar - SlideShare
SlideShare
 
What to Upload to SlideShare
What to Upload to SlideShareWhat to Upload to SlideShare
What to Upload to SlideShare
SlideShare
 
How to Make Awesome SlideShares: Tips & Tricks
How to Make Awesome SlideShares: Tips & TricksHow to Make Awesome SlideShares: Tips & Tricks
How to Make Awesome SlideShares: Tips & Tricks
SlideShare
 
Getting Started With SlideShare
Getting Started With SlideShareGetting Started With SlideShare
Getting Started With SlideShare
SlideShare
 

Viewers also liked (11)

What Makes Great Infographics
What Makes Great InfographicsWhat Makes Great Infographics
What Makes Great Infographics
 
Masters of SlideShare
Masters of SlideShareMasters of SlideShare
Masters of SlideShare
 
STOP! VIEW THIS! 10-Step Checklist When Uploading to Slideshare
STOP! VIEW THIS! 10-Step Checklist When Uploading to SlideshareSTOP! VIEW THIS! 10-Step Checklist When Uploading to Slideshare
STOP! VIEW THIS! 10-Step Checklist When Uploading to Slideshare
 
You Suck At PowerPoint!
You Suck At PowerPoint!You Suck At PowerPoint!
You Suck At PowerPoint!
 
10 Ways to Win at SlideShare SEO & Presentation Optimization
10 Ways to Win at SlideShare SEO & Presentation Optimization10 Ways to Win at SlideShare SEO & Presentation Optimization
10 Ways to Win at SlideShare SEO & Presentation Optimization
 
How To Get More From SlideShare - Super-Simple Tips For Content Marketing
How To Get More From SlideShare - Super-Simple Tips For Content MarketingHow To Get More From SlideShare - Super-Simple Tips For Content Marketing
How To Get More From SlideShare - Super-Simple Tips For Content Marketing
 
A Guide to SlideShare Analytics - Excerpts from Hubspot's Step by Step Guide ...
A Guide to SlideShare Analytics - Excerpts from Hubspot's Step by Step Guide ...A Guide to SlideShare Analytics - Excerpts from Hubspot's Step by Step Guide ...
A Guide to SlideShare Analytics - Excerpts from Hubspot's Step by Step Guide ...
 
2015 Upload Campaigns Calendar - SlideShare
2015 Upload Campaigns Calendar - SlideShare2015 Upload Campaigns Calendar - SlideShare
2015 Upload Campaigns Calendar - SlideShare
 
What to Upload to SlideShare
What to Upload to SlideShareWhat to Upload to SlideShare
What to Upload to SlideShare
 
How to Make Awesome SlideShares: Tips & Tricks
How to Make Awesome SlideShares: Tips & TricksHow to Make Awesome SlideShares: Tips & Tricks
How to Make Awesome SlideShares: Tips & Tricks
 
Getting Started With SlideShare
Getting Started With SlideShareGetting Started With SlideShare
Getting Started With SlideShare
 

Similar to Slide Web Service

C# co ban 9
C# co ban 9C# co ban 9
C# co ban 9
baotuyet_113
 
Asp
AspAsp
Asp
thinhtu
 
B tl internet
B tl internetB tl internet
B tl internettoan
 
Bài 1: Làm quen với ASP.NET - Giáo trình FPT - Có ví dụ kèm theo
Bài 1: Làm quen với ASP.NET - Giáo trình FPT - Có ví dụ kèm theoBài 1: Làm quen với ASP.NET - Giáo trình FPT - Có ví dụ kèm theo
Bài 1: Làm quen với ASP.NET - Giáo trình FPT - Có ví dụ kèm theo
MasterCode.vn
 
ứNg dụng xml
ứNg dụng xmlứNg dụng xml
ứNg dụng xml
Son Nguyen
 
Giao trinh asp.ne_tvoi_csharp
Giao trinh asp.ne_tvoi_csharpGiao trinh asp.ne_tvoi_csharp
Giao trinh asp.ne_tvoi_csharpngohanty13
 
Introduction Vs2008 Dot Net35
Introduction Vs2008 Dot Net35Introduction Vs2008 Dot Net35
Introduction Vs2008 Dot Net35
TechMaster Vietnam
 
Web Architecture
Web ArchitectureWeb Architecture
Web Architecture
Hiep Luong
 
Giáo trình asp.net với c sharp
Giáo trình asp.net với c sharpGiáo trình asp.net với c sharp
Giáo trình asp.net với c sharp
Trần Văn Sáng Trần
 
Php 01 modau
Php 01 modauPhp 01 modau
Php 01 modau
Vương Nhung
 
Business process excution language
Business process excution languageBusiness process excution language
Business process excution languageNguyen Tran
 
Bai44-48.pptx
Bai44-48.pptxBai44-48.pptx
Bai44-48.pptx
BuiManhHung3
 
Luận văn: Kỹ thuật điều tra phân tích tấn công web, HAY
Luận văn: Kỹ thuật điều tra phân tích tấn công web, HAYLuận văn: Kỹ thuật điều tra phân tích tấn công web, HAY
Luận văn: Kỹ thuật điều tra phân tích tấn công web, HAY
Dịch Vụ Viết Thuê Khóa Luận Zalo/Telegram 0917193864
 
Bai1 gioi thieu_servlet_va_jsp_8952
Bai1 gioi thieu_servlet_va_jsp_8952Bai1 gioi thieu_servlet_va_jsp_8952
Bai1 gioi thieu_servlet_va_jsp_8952
Ham Chơi
 
Chuong 1 tongquanve web&amp;htm-lcanban
Chuong 1  tongquanve web&amp;htm-lcanban Chuong 1  tongquanve web&amp;htm-lcanban
Chuong 1 tongquanve web&amp;htm-lcanban
Quý Nguyễn
 
Chuong 1 tongquanve web&amp;htm-lcanban
Chuong 1  tongquanve web&amp;htm-lcanban Chuong 1  tongquanve web&amp;htm-lcanban
Chuong 1 tongquanve web&amp;htm-lcanban
Quý Nguyễn
 
Chương 2.pdf
Chương 2.pdfChương 2.pdf
Chương 2.pdf
HoaNguynTh48
 

Similar to Slide Web Service (20)

Giới thiệu WCF
Giới thiệu WCFGiới thiệu WCF
Giới thiệu WCF
 
Asp control
Asp controlAsp control
Asp control
 
C# co ban 9
C# co ban 9C# co ban 9
C# co ban 9
 
Asp
AspAsp
Asp
 
B tl internet
B tl internetB tl internet
B tl internet
 
Ung dun web chuong 2
Ung dun web  chuong 2Ung dun web  chuong 2
Ung dun web chuong 2
 
Bài 1: Làm quen với ASP.NET - Giáo trình FPT - Có ví dụ kèm theo
Bài 1: Làm quen với ASP.NET - Giáo trình FPT - Có ví dụ kèm theoBài 1: Làm quen với ASP.NET - Giáo trình FPT - Có ví dụ kèm theo
Bài 1: Làm quen với ASP.NET - Giáo trình FPT - Có ví dụ kèm theo
 
ứNg dụng xml
ứNg dụng xmlứNg dụng xml
ứNg dụng xml
 
Giao trinh asp.ne_tvoi_csharp
Giao trinh asp.ne_tvoi_csharpGiao trinh asp.ne_tvoi_csharp
Giao trinh asp.ne_tvoi_csharp
 
Introduction Vs2008 Dot Net35
Introduction Vs2008 Dot Net35Introduction Vs2008 Dot Net35
Introduction Vs2008 Dot Net35
 
Web Architecture
Web ArchitectureWeb Architecture
Web Architecture
 
Giáo trình asp.net với c sharp
Giáo trình asp.net với c sharpGiáo trình asp.net với c sharp
Giáo trình asp.net với c sharp
 
Php 01 modau
Php 01 modauPhp 01 modau
Php 01 modau
 
Business process excution language
Business process excution languageBusiness process excution language
Business process excution language
 
Bai44-48.pptx
Bai44-48.pptxBai44-48.pptx
Bai44-48.pptx
 
Luận văn: Kỹ thuật điều tra phân tích tấn công web, HAY
Luận văn: Kỹ thuật điều tra phân tích tấn công web, HAYLuận văn: Kỹ thuật điều tra phân tích tấn công web, HAY
Luận văn: Kỹ thuật điều tra phân tích tấn công web, HAY
 
Bai1 gioi thieu_servlet_va_jsp_8952
Bai1 gioi thieu_servlet_va_jsp_8952Bai1 gioi thieu_servlet_va_jsp_8952
Bai1 gioi thieu_servlet_va_jsp_8952
 
Chuong 1 tongquanve web&amp;htm-lcanban
Chuong 1  tongquanve web&amp;htm-lcanban Chuong 1  tongquanve web&amp;htm-lcanban
Chuong 1 tongquanve web&amp;htm-lcanban
 
Chuong 1 tongquanve web&amp;htm-lcanban
Chuong 1  tongquanve web&amp;htm-lcanban Chuong 1  tongquanve web&amp;htm-lcanban
Chuong 1 tongquanve web&amp;htm-lcanban
 
Chương 2.pdf
Chương 2.pdfChương 2.pdf
Chương 2.pdf
 

Slide Web Service

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