150 likes | 270 Views
Web Services Introduction. ISYS 546. Web Service. XML Web Service Web services are classes that are stored on the web which can instantiate and use in both Windows and Web applications. Creating a Web Service Using a Text Editor. WebService directive: <%@ WebService Class="CustomerInfo" %>
E N D
Web Services Introduction ISYS 546
Web Service • XML Web Service • Web services are classes that are stored on the web which can instantiate and use in both Windows and Web applications.
Creating a Web Service Using a Text Editor • WebService directive: • <%@ WebService Class="CustomerInfo" %> • Import namespaces: • imports System.Web.Services • imports System • Define the web service class. • Add WebMethod attribute to the function declaration. • Note: Web service file extension is ASMX
A Web Service ExampleASPET/TestWebService.ASMX <%@ WebService Class="CustomerInfo" %> imports System.Web.Services imports System imports System.Data imports System.Data.Oledb Public Class CustomerInfo <webMethod()> public Function GetCname(ByVal CID as String) as String dim strConn as string ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" dim objConn as new OledbConnection(strConn) dim strSQL as string = "select * from customer where CID = '" & CID & "';" dim objComm as new OledbCommand(strSQL,objConn) dim Results as string objConn.open() dim objDataReader as oledbDataReader objDataReader=objComm.executeReader() objDataReader.read() return objDataReader("Cname") end function
Creating a Web Service Using VS • New Project/ASP.Net Web Service
Web Service Description Language (WSDL) • A WSDL file is an XML document containing a complete description of the web service. It shows a web service’s name, methods, and parameter types. • Help page: After entering web service’s URL, a help page is displayed. You can click the Service Description link to see the WSDL file.
HTTP • HTTP Request: • Request line: Method – Get, Post • Header • Body • HTTP Response: • Respons line: HTTP version and status code • Header • Body
Invoking a Web Service with HTTP • Http – Get: • http://dchaolaptop/aspnet/testwebservice.asmx/GetRating?cid=1 • Http – Post: • Use the Action attribute to call the function. • Http-SOAP: Use Http’s Post method to send an XML document in Https’ body section. • Demo: Help page
Simple Object Access ProtocolSOAP • SOAP Request and Response message: • Envelop: An envelop element surrounds the message. It is the root element of a SOAP XML document. • Body: • Request: Contains the method call name and parameter names and values. • Response: Contains the results returned by the method.
Consuming Web Services from a Windows Application • Add a web reference to the web service. • Declare a web service class variable. • Dim myWebService As New dchaolaptop.CustomerInfo() • Demo: UseWebService
Universal Description, Discovery, and Integration (UDDI) • A directory service for web services. • http://uddi.org
Consuming Web Services from a Web Application • Creating a web service proxy class: • A local representation of the web service. • Steps to creating a proxy class: • Use the Wsdl.exe tool to generate the source code file for the proxy class. • Compile the source code file into a DLL file. • Copy the DLL file to the application’s Bin folder.
Use the Wsdl.Exe Tool to Generate the Source Code File • Issue the wsdl.exe command at the Visual Studio command prompt: Start/Programs/microsoft VS .Net/VS .Net Tools/ VS .Net Command prompt The following command example create a CustomerInfo.vb file for the testwebservice.asmx web service. wsdl.exe /l:vb http://dchaolaptop/aspnet/testwebservice.asmx?wsdl
Compile the Source Code File Into a DLL File • The command below create a CustomerInfo.dll Vbc /t:library /r:system.dll,system.web.services.dll,system.xml.dll CustomerInfo.vb
<script runat="server"> sub CallService(s as object, e as eventArgs) dim myProxy as new CustomerInfo lblCname.text=myProxy.GetCname(txtCID.text) lblRating.text=myProxy.GetRating(txtCID.text) end sub </script> <html> </head> <body> <form runat="server"> Enter CID: <asp:textbox id="txtCID" runat="server" /> <asp:label id="lblCname" runat="server"/> <asp:label id="lblRating" runat="server"/> <asp:button text="GetService" onClick="CallService" runat="server" /> </form> </body> </html> Demo: testProxy.aspx