1. Trang chủ
  2. » Giáo án - Bài giảng

Bài 10: Thiết kê và lập trình web với ASP.NET - Web services

9 535 2

Đ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 9
Dung lượng 896,37 KB

Nội dung

Thitk & Lp trình Web 2 Bài 10 Web ServicesWeb Services Ths. TrnTh Bích Hnh Khoa CNTT Khoa CNTT –– H.KHTNH.KHTN © 2009 Khoa Công ngh thông tin Thitk & Lp trình Web 2 – Bài 10: Web Services NiNi dungdung NiNi dungdung  Giithi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tk & Lp trình Web 2 – Bài 10: Web Services NiNi dungdung NiNi dungdung  Giithiu Web Services  Kin trúc Web Services  Lp trình Web Services bng VS.NET © 2009 Khoa CNTT - HKHTN 3 Thitk & Lp trình Web 2 – Bài 10: Web Services LàmLàm saosao thanhthanh toántoán hóahóa đnđn đinđin thoithoi online?online? LàmLàm saosao thanhthanh toántoán hóahóa đnđn đinđin thoithoi online?online? 1 3 Telec om Internet 2 Pay Bill PBill L thô ti Hó đ t Tl Bank P ay Bill L y thô ng ti n Hó a đ n t T e l ecom 1 Pay Bill Yêu cu Bank chuynkhont Customer Account sang Telecom Account 2 © 2009 Khoa CNTT - HKHTN 4 Pay Bill Thông báo cho Teleco m bitcpnht tình trng hóa đn đã thanh toán 3 Thitk & Lp trình Web 2 – Bài 10: Web Services Web ServiceWeb Service Web ServiceWeb Service  Là mttp các phng thc đcgithchint xa thông qua mt đa ch URL thông qua mt đa ch URL 1 Internet Telecom Ser ice 1 3 Telecom Ser v ice 2 3 Pay Bill Bank Service 2 © 2009 Khoa CNTT - HKHTN 5 Thitk & Lp trình Web 2 – Bài 10: Web Services cc đimđim Web ServicesWeb Services cc đimđim Web ServicesWeb Services  T mô t  Datrênnntng XML  Có thđcs dng bicácng dng khác  Chi phí phát trinthp  D botrì © 2009 Khoa CNTT - HKHTN 6 Thitk & Lp trình Web 2 – Bài 10: Web Services NiNi dungdung NiNi dungdung  Giithiu Web Services  Kin trúc Web Services  Lp trình Web Services bng VS.NET © 2009 Khoa CNTT - HKHTN 7 Thitk & Lp trình Web 2 – Bài 10: Web Services KinKin trúctrúc Web ServicesWeb Services KinKin trúctrúc Web ServicesWeb Services UDDI (Web Service Broker) Service Broker) Publish Find Internet Bind Web Service Provider Web Service Consumer © 2009 Khoa CNTT - HKHTN 8 Consumer Thitk & Lp trình Web 2 – Bài 10: Web Services KinKin trúctrúc caca Web ServicesWeb Services KinKin trúctrúc caca Web ServicesWeb Services UDDI WSDL SOAP SOAP IIS A n y Client SOAP Web Service © 2009 Khoa CNTT - HKHTN 9 y Thitk & Lp trình Web 2 – Bài 10: Web Services StandardsStandards StandardsStandards • 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 WSDL (W b Si Diti L) • WSDL (W e b S erv i ce D escr i p ti on L anguage ) – Ngôn ngđct web service • UDDI (Universal Description Discovery and Integration) – Th mc lu tr thông tin v Web services Th mc lu tr 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tk & Lp trình Web 2 – Bài 10: Web Services SOAPSOAP SOAPSOAP • Simple Object Access Protocol  – 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 – X lý • Re q uestor g ói thôn g tin y êu cuvàom  t messa g e g i q g g y  g g lên web service • Web Service phân tích message gi lên thc thi hàm x l ý y êu cu. ý y • Ktquđc gói vào mt message giv cho Requestor • Requestor phân tích message tr v hin th kt qu © 2009 Khoa CNTT - HKHTN Requestor phân tích message tr v hin th kt qu – ct: http://www.w3.org/TR/soap 11 Thitk & Lp trình Web 2 – Bài 10: Web Services VíVí dd SOAPSOAP VíVí dd SOAPSOAP © 2009 Khoa CNTT - HKHTN 12 Thitk & Lp trình Web 2 – Bài 10: Web Services WSDLWSDL WSDLWSDL • Web Service Description Language – Ngôn ngđct web service – ct: http://www.w3.org/TR/wsdl © 2009 Khoa CNTT - HKHTN 13 Thitk & Lp trình Web 2 – Bài 10: Web Services WSDL ExampleWSDL Example WSDL ExampleWSDL Example © 2009 Khoa CNTT - HKHTN 14 Thitk & Lp trình Web 2 – Bài 10: Web Services UDDIUDDI UDDIUDDI • Universal Description Discovery and Integration  – Th mclutr thông tin v  web services – Cho phép doanh nghip đng ký service camìnhvào danh sách các services tìm kim service ca nhng danh sách các services tìm kim service ca nhng doanh nghip khác đ s dng – ct bng WSDL – Giao tip thông qua SOAP – cxâydng trong Microsoft .NET platform – ct: http://www.uddi.org/pubs/uddi_v3.htm © 2009 Khoa CNTT - HKHTN 15 Thitk & Lp trình Web 2 – Bài 10: Web Services NiNi dungdung NiNi dungdung  Giithiu Web Services  Kin trúc Web Services  Lp trình Web Services bng VS.NET © 2009 Khoa CNTT - HKHTN 16 Thitk & Lp trình Web 2 – Bài 10: Web Services Mô hình tng tác gia các thành phnMô hình tng tác gia các thành phn Mô hình tng tác gia các thành phnMô hình tng tác gia các thành phn 1 5 1 Pricing Web Service UDDI Services 3 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pnht 4 © 2009 Khoa CNTT - HKHTN 17 Web service consumer gi& nhnktqu t web service 5 Thitk & Lp trình Web 2 – Bài 10: Web Services LptrìnhWebservicebng VS NETLptrìnhWebservicebng VS NET Lp trình Web service bng VS . NETLp trình Web service bng VS . NET Web service programming - provider side 1 1 Web Service Provider Response Request Web Service Consumer Request © 2009 Khoa CNTT - HKHTN 18 Web service programming - consumer side 2 2 Thitk & Lp trình Web 2 – Bài 10: Web Services Web Service Provider SideWeb Service Provider Side Web Service Provider SideWeb Service Provider Side  Loi Project – New Website Web Service Provider – ASP.NET Web Service hoc – ASP.NET Web Application © 2009 Khoa CNTT - HKHTN 19 Thitk & Lp trình Web 2 – Bài 10: Web Services Web Service ClassWeb Service Class Web Service ClassWeb Service Class // Service.asmx <%@ WebService Language="C#" CodeBehind="~/App_Code/Service.cs" Cl " Si " %> // Service.asmx <%@ WebService Language="C#" CodeBehind="~/App_Code/Service.cs" Cl " Si " %> Cl ass= " S erv i ce " %> Cl ass= " S erv i ce " %> // Service.cs// 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 () { } pu bli c S erv i ce () { } [WebMethod] public string HelloWorld() { return "Hello World"; pu bli c S erv i ce () { } [WebMethod] public string HelloWorld() { return "Hello World"; © 2009 Khoa CNTT - HKHTN 20 } } } } Thitk & Lp trình Web 2 – Bài 10: Web Services KhaiKhai báobáo phngphng thcthc Web ServiceWeb Service KhaiKhai báobáo phngphng thcthc Web ServiceWeb Service  B sung ch th [WebMethod] trc khai báo hàm Cá th  tí h  [ WbMthd ]  Cá c th u  c tí n h c  a [ W e bM e th o d ] – BufferResponse – CacheDuration – Description – EnableSession TtiOti [WebMethod(BufferResponse=false)] pub li c Tr a n sact i o n s Get Tr a n sact i o nHi sto r y() [WebMethod(BufferResponse=false)] pub li c Tr a n sact i o n s Get Tr a n sact i o nHi sto r y() – T ransac ti on O p ti on 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; } Thitk & Lp trình Web 2 – Bài 10: Web Services XML SerializationXML Serialization XML SerializationXML Serialization  Dùng đ xác đnh cutrúctàili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 XmlAttribute XmlInclude © 2009 Khoa CNTT - HKHTN 22 Thitk & Lp trình Web 2 – Bài 10: Web Services VíVí dd 11 –– XmlRootXmlRoot && XmlElementXmlElement VíVí dd 1 1 –– XmlRootXmlRoot & & XmlElementXmlElement <account status=“123”> [XmlRoot("account")] public class Acct { [ XmlEl e m e nt ( " desc ri p ti o n" )] <description>Hello</description> <number>10 </number> <type>C</type> <account> [ ee ( desc p o )] public string Description; [ XmlElement("number")] public string Number; [ XmlElement ( "t yp e" )] [ (yp )] public string Type; [ XmlElement("balance")] public decimal Balance; [ XmlAttribute ( "status" )] [ ()] public string Status; } © 2009 Khoa CNTT - HKHTN 23 Thitk & Lp trình Web 2 – Bài 10: Web Services VíVí dd 22 –– XmlArrayXmlArray && XmlArrayItemXmlArrayItem VíVí dd 2 2 –– XmlArrayXmlArray & & XmlArrayItemXmlArrayItem … [return: XmlArray("AccountList")] [return: XmlArrayItem("Account")] <AccountList> <account> public A cct[] GetAllAccounts () … <account> ……… <account> <account> …. </account> <AccountList> © 2009 Khoa CNTT - HKHTN 24 Thitk & Lp trình Web 2 – Bài 10: Web Services VíVí dd 33 –– XmlArrayItemXmlArrayItem && ElementNameElementName VíVí dd 3 3 –– XmlArrayItemXmlArrayItem & & ElementNameElementName … [return:XmlArrayItem(ElementName="savingsAcct", Type=typeof(SavingsAcct))] [return: XmlArrayItem(ElementName="creditCardAcct", Type=typeof(CreditCardAcct))] [WebMethod] public Acct[] GetAllAccounts () { … } public Acct[] GetAllAccounts () { … } AtLit < A ccoun tLi s t > … <SavingAcct> </SavingActt> … <CreditCardAcct> </CreditCardActt> … /A tLi t © 2009 Khoa CNTT - HKHTN 25 < /A ccoun tLi s t > Thitk & Lp trình Web 2 – Bài 10: Web Services VíVí dd 44 XmlIncludeXmlInclude VíVí dd 4 4 XmlIncludeXmlInclude public class Acct { [WebMethod] { public string Description; public string Number; p ublic strin g T yp e ; [XmlInclude(typeof(CreditCardAcct))] [ XmlInclude(typeof(SavingsAcct))] [return: XmlArray("AccountList")] [return: XmlArrayItem ( " Account " )] pgyp; public decimal Balance; public string Status; } bli l Si At At [return: XmlArrayItem ( Account )] public Acct[] GetAllAccounts() { SavingsAcct a = new SavingsAcct(); CditCdAt pu bli c c l ass S av i ngs A cc t : A cc t { public decimal MinimumBalance; } C re ditC ar dA cc t cc = new CreditCardAcct(); // populate the accounts A cc t [] sa = n e w A cc t [ 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tk & Lp trình Web 2 – Bài 10: Web Services TrinTrin khaikhai Web ServiceWeb Service TrinTri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 Director y cho “Web Servicce Precom p lied Director y ” nh *.xml  y p y trin khai mt ng dng Web  Xem WSDL Document ca 1 web service http://localhost/WebService/service asmx?wsdl © 2009 Khoa CNTT - HKHTN http://localhost/WebService/service . asmx?wsdl 27 Thitk & Lp trình Web 2 – Bài 10: Web Services Web Service Consumer SideWeb Service Consumer Side Web Service Consumer SideWeb Service Consumer Side  LoiProject – ASP.NET Web Service – ASP.NET Web Application Wi d A li i – Wi n d ow A pp li cat i on – … Web Service Consumer  Gi Web Service – S dng Proxy Class Web Service Consumer © 2009 Khoa CNTT - HKHTN 28 Thitk & Lp trình Web 2 – Bài 10: Web Services Application ModelApplication Model Application ModelApplication Model Web Service Developer 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 Thitk & Lp trình Web 2 – Bài 10: Web Services Proxy classProxy class Proxy classProxy class  Lpgiaotipvi Web Service  c phát sinh t WSDL document  cs dng đ gi các phng thcca Web Service © 2009 Khoa CNTT - HKHTN 30 Thitk & Lp trình Web 2 – Bài 10: Web Services Add Web ReferenceAdd Web Reference Add Web ReferenceAdd Web Reference © 2009 Khoa CNTT - HKHTN 31 Thitk & Lp trình Web 2 – Bài 10: Web Services GiGi thcthc thithi Web ServiceWeb Service GiGi thcthc thithi Web ServiceWeb Service // Default.aspx.cs WebService.Service service = new WebService.Service (); © 2009 Khoa CNTT - HKHTN 32 (); string str = service.HelloWorld(); Thitk & Lp trình Web 2 – Bài 10: Web Services Web serviceWeb 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 Session đ qun lý trng thái ca web service © 2009 Khoa CNTT - HKHTN 33 Thitk & Lp trình Web 2 – Bài 10: Web Services TngTng ktkt TngTng ktkt  Web Services  Kin trúc Web ServicesWeb Service Provider – Web Service Consumer – Web Service Broker  Web Service Standards – SOAP – WSDL – UDDI © 2009 Khoa CNTT - HKHTN  Lp trình Web Services bng VS.NET 34 . 2009 Khoa CNTT - HKHTN 7 Thitk & Lp trình Web 2 – Bài 10: Web Services KinKin trúctrúc Web ServicesWeb Services KinKin trúctrúc Web ServicesWeb Services UDDI (Web Service Broker) Service. Service 2 © 2009 Khoa CNTT - HKHTN 5 Thitk & Lp trình Web 2 – Bài 10: Web Services cc đimđim Web ServicesWeb Services cc đimđim Web ServicesWeb Services  T mô t  Datrênnntng. Services NiNi dungdung NiNi dungdung  Giithi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tk & Lp trình Web 2 – Bài 10: Web Services NiNi dungdung NiNi dungdung 

Ngày đăng: 09/05/2014, 14:41

TỪ KHÓA LIÊN QUAN