Thitk & Lp trình Web 2 Bài 10 Web ServicesWeb Services Ths. TrnTh Bích Hnh Khoa CNTT Khoa CNTT –– H.KHTNH.KHTN © 2009 Khoa Công ngh thông tin Thitk & Lp trình Web 2 – Bài 10: Web Services NiNi dungdung NiNi dungdung Giithiu Web Services Kin trúc Web Services Lp trình Web Services bng VS.NET © 2009 Khoa CNTT - HKHTN 2 Thitk & Lp trình Web 2 – Bài 10: Web Services NiNi dungdung NiNi dungdung Giithiu Web Services Kin trúc Web Services Lp trình Web Services bng VS.NET © 2009 Khoa CNTT - HKHTN 3 Thitk & Lp trình Web 2 – Bài 10: Web Services LàmLàm saosao thanhthanh toántoán hóahóa đnđn đinđin thoithoi online?online? LàmLàm saosao thanhthanh toántoán hóahóa đnđn đinđin thoithoi 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 cu Bank chuynkhont Customer Account sang Telecom Account 2 © 2009 Khoa CNTT - HKHTN 4 Pay Bill Thông báo cho Teleco m bitcpnht tình trng hóa đn đã thanh toán 3 Thitk & Lp trình Web 2 – Bài 10: Web Services Web ServiceWeb Service Web ServiceWeb Service Là mttp các phng thc đcgithchint xa thông qua mt đa ch URL thông qua mt đ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 Thitk & Lp trình Web 2 – Bài 10: Web Services cc đimđim Web ServicesWeb Services cc đimđim Web ServicesWeb Services T mô t Datrênnntng XML Có thđcs dng bicácng dng khác Chi phí phát trinthp D botrì © 2009 Khoa CNTT - HKHTN 6 Thitk & Lp trình Web 2 – Bài 10: Web Services NiNi dungdung NiNi dungdung Giithiu Web Services Kin trúc Web Services Lp trình Web Services bng VS.NET © 2009 Khoa CNTT - HKHTN 7 Thitk & Lp trình Web 2 – Bài 10: Web Services KinKin trúctrúc Web ServicesWeb Services KinKin 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 Thitk & Lp trình Web 2 – Bài 10: Web Services KinKin trúctrúc caca Web ServicesWeb Services KinKin trúctrúc caca Web ServicesWeb Services UDDI WSDL SOAP SOAP IIS A n y Client SOAP Web Service © 2009 Khoa CNTT - HKHTN 9 y Thitk & Lp trình Web 2 – Bài 10: Web Services StandardsStandards StandardsStandards • SOAP (Simple Object Access Protocol) Giao thc trao đi thông tin ca service theo đnh dng XML – Giao thc trao đi thông tin ca service theo đnh dng 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 mc lu tr thông tin v Web services Th mc lu tr thông tin v Web services – Cho phép các công ty đng ký & tìm kim web services © 2009 Khoa CNTT - HKHTN 10 Thitk & Lp trình Web 2 – Bài 10: Web Services SOAPSOAP SOAPSOAP • Simple Object Access Protocol – Cho phép truy n các thông đip (message) qua giao thc HTTP – S dng XML đ mô t d liu – S dng XML đ mô t d liu – X lý • Re q uestor g ói thôn g tin y êu cuvàom t messa g e và g i q g g y g g lên web service • Web Service phân tích message gi lên và thc thi hàm x l ý y êu cu. ý y • Ktquđc gói vào mt message và giv cho Requestor • Requestor phân tích message tr v và hin th kt qu © 2009 Khoa CNTT - HKHTN Requestor phân tích message tr v và hin th kt qu – ct: http://www.w3.org/TR/soap 11 Thitk & Lp trình Web 2 – Bài 10: Web Services VíVí dd SOAPSOAP VíVí dd SOAPSOAP © 2009 Khoa CNTT - HKHTN 12 Thitk & Lp 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 Thitk & Lp trình Web 2 – Bài 10: Web Services WSDL ExampleWSDL Example WSDL ExampleWSDL Example © 2009 Khoa CNTT - HKHTN 14 Thitk & Lp trình Web 2 – Bài 10: Web Services UDDIUDDI UDDIUDDI • Universal Description Discovery and Integration – Th mclutr thông tin v web services – Cho phép doanh nghip đng ký service camìnhvào danh sách các services và tìm kim service ca nhng danh sách các services và tìm kim service ca nhng doanh nghip khác đ s dng – ct bng WSDL – Giao tip thông qua SOAP – cxâydng trong Microsoft .NET platform – ct: http://www.uddi.org/pubs/uddi_v3.htm © 2009 Khoa CNTT - HKHTN 15 Thitk & Lp trình Web 2 – Bài 10: Web Services NiNi dungdung NiNi dungdung Giithiu Web Services Kin trúc Web Services Lp trình Web Services bng VS.NET © 2009 Khoa CNTT - HKHTN 16 Thitk & Lp trình Web 2 – Bài 10: Web Services Mô hình tng tác gia các thành phnMô hình tng tác gia các thành phn Mô hình tng tác gia các thành phnMô hình tng tác gia các thành phn 1 5 1 Pricing Web Service UDDI Services 3 4 2 Web service developer xây dng & trin khai “ pricing ” Web service 1 4 Web service developer xây dng & trin khai pricing Web service 1 Web service developer đng ký & phân loi Web service 2 Web service consumer truy xut UDDI đ tìm “pricing” services 3 Web service consumer chn “pricing” service phù hpnht 4 © 2009 Khoa CNTT - HKHTN 17 Web service consumer gi& nhnktqu t web service 5 Thitk & Lp trình Web 2 – Bài 10: Web Services LptrìnhWebservicebng VS NETLptrìnhWebservicebng VS NET Lp trình Web service bng VS . NETLp trình Web service bng 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 Thitk & Lp trình Web 2 – Bài 10: Web Services Web Service Provider SideWeb Service Provider Side Web Service Provider SideWeb Service Provider Side Loi Project – New Website Web Service Provider – ASP.NET Web Service hoc – ASP.NET Web Application © 2009 Khoa CNTT - HKHTN 19 Thitk & Lp 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 } } } } Thitk & Lp trình Web 2 – Bài 10: Web Services KhaiKhai báobáo phngphng thcthc Web ServiceWeb Service KhaiKhai báobáo phngphng thcthc Web ServiceWeb Service B sung ch th [WebMethod] trc 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; } Thitk & Lp trình Web 2 – Bài 10: Web Services XML SerializationXML Serialization XML SerializationXML Serialization Dùng đ xác đnh cutrúctàiliu XML mong mun đ biu din đi tng ca mt lp biu din đi tng ca mt lp XmlArray Namespace : System.Xml.Serialization XmlElement XmlRoot XmlArrayItem XmlArray XmlAttribute XmlInclude © 2009 Khoa CNTT - HKHTN 22 Thitk & Lp 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 { [ 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 Thitk & Lp 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 A cct[] GetAllAccounts () … <account> ……… <account> <account> …. </account> <AccountList> © 2009 Khoa CNTT - HKHTN 24 Thitk & Lp trình Web 2 – Bài 10: Web Services VíVí 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 () { … } 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 > Thitk & Lp trình Web 2 – Bài 10: Web Services VíVí dd 44 XmlIncludeXmlInclude VíVí dd 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 } } Thitk & Lp trình Web 2 – Bài 10: Web Services TrinTrin khaikhai Web ServiceWeb Service TrinTrin 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 trin khai mt ng dng Web Xem WSDL Document ca 1 web service http://localhost/WebService/service asmx?wsdl © 2009 Khoa CNTT - HKHTN http://localhost/WebService/service . asmx?wsdl 27 Thitk & Lp trình Web 2 – Bài 10: Web Services Web Service Consumer SideWeb Service Consumer Side Web Service Consumer SideWeb Service Consumer Side LoiProject – 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 Gi Web Service – S dng Proxy Class Web Service Consumer © 2009 Khoa CNTT - HKHTN 28 Thitk & Lp 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 Thitk & Lp trình Web 2 – Bài 10: Web Services Proxy classProxy class Proxy classProxy class Lpgiaotipvi Web Service c phát sinh t WSDL document cs dng đ gi các phng thcca Web Service © 2009 Khoa CNTT - HKHTN 30 Thitk & Lp trình Web 2 – Bài 10: Web Services Add Web ReferenceAdd Web Reference Add Web ReferenceAdd Web Reference © 2009 Khoa CNTT - HKHTN 31 Thitk & Lp trình Web 2 – Bài 10: Web Services GiGi thcthc thithi Web ServiceWeb Service GiGi thcthc thithi Web ServiceWeb Service // Default.aspx.cs WebService.Service service = new WebService.Service (); © 2009 Khoa CNTT - HKHTN 32 (); string str = service.HelloWorld(); Thitk & Lp 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 dng đi tng Application và Session đ qun lý trng thái ca web service © 2009 Khoa CNTT - HKHTN 33 Thitk & Lp trình Web 2 – Bài 10: Web Services TngTng ktkt TngTng ktkt Web Services Kin trúc Web Services – Web Service Provider – Web Service Consumer – Web Service Broker Web Service Standards – SOAP – WSDL – UDDI © 2009 Khoa CNTT - HKHTN Lp trình Web Services bng VS.NET 34 . 2009 Khoa CNTT - HKHTN 7 Thitk & Lp trình Web 2 – Bài 10: Web Services KinKin trúctrúc Web ServicesWeb Services KinKin trúctrúc Web ServicesWeb Services UDDI (Web Service Broker) Service. Service 2 © 2009 Khoa CNTT - HKHTN 5 Thitk & Lp trình Web 2 – Bài 10: Web Services cc đimđim Web ServicesWeb Services cc đimđim Web ServicesWeb Services T mô t Datrênnntng. Services NiNi dungdung NiNi dungdung Giithiu Web Services Kin trúc Web Services Lp trình Web Services bng VS.NET © 2009 Khoa CNTT - HKHTN 2 Thitk & Lp trình Web 2 – Bài 10: Web Services NiNi dungdung NiNi dungdung