Web Services in.net (1) These slides are meant to be for teaching purposes only and only for the students that are registered in CSE4413 and should not be published as a book or in any form of commercial product, unless written permission is obtained. 1
What is a Web Service? A Web Service (WS) is a class (or many classes) that resides in a computer and it is accessible from another computer, in the sense that clients can call methods of this class remotely. In.NET, any method of a class can be equipped to be accessed remotely. All it needs is to associate the attribute [WebMethod] with that method. [ WebMethod ] public void MyMethod( ) { } 2
Web Services vs Local Services Web service Local service Class produces DLL file which is stored in local computer, and it is accessed from classes residing in some other computer Access is done via remote system calls managed by HTTP and SOAP (Simple Object Access Protocol) a protocol that allows calling remote methods). SOAP is written in XML. Class produces DLL file which is stored in local computer, and it is accessed from classes residing in same computer. Access is done via local system calls (managed by the local operating system). System calls are written in some language (part of the OS). 3
Web Services vs Local Services / Web service Documentation for a web method is provided via WSDL (Web Service Description Language). Local service Documentation for a local method is provided via a local API (Application Programming Interface). WSDL is written in XML. API may be written in XML. We learn about Web Services via UDDI (Universal Description Discovery and Integration) and DISCO. [ in practice, however, so far, we learn mostly through word-of-mouth ] We learn about local methods via the API. 4
Evolution of the internet and the WWW (0) HTML Generation 0 FTP, e-mail 5
Evolution of the web (1) HTML Generation 1 Static HTML 6
Evolution of the web (2) HTML Generation 2 Web Applications 7
Evolution of the web (3) HTML, XML HTML, XML Generation 3 (under construction) Web Services 8
Evolution of the web (4) HTML, XML empid HTML, XML, RDF Generation 4 (under contemplation) Semantic Web Emp-id Check and be able to realize that empid and Emp-id mean the same thing. 9
Benefits of Web Services May facilitate significant boost for developing distributed applications. Use standard (SOAP) for message exchange. Use standard for interface description (WSDL), in XML. Independent of programming languages and operating systems. Utilize existing Internet protocols (HTTP usually, but also SMTP and FTP). 10
Benefits of Web Services / Web Services allow you to interconnect: Different companies Many/any devices Applications Different clients Not just browsers Distribution and integration of application logic Enable the programmable Web Not just the purely interactive Web Loosely coupled (scale well) 11
Comparison of various distributed (Web Services and ancestors) technologies Java RMI.NET Remoting CORBA Web services Programming language Java.NET languages (C#, VB.NET,..) independent independent Interface definition Java Interfaces.NET Interfaces CORBA IDL WSDL (XMLbased) Data exchange format Java objects (binary).net objects IDL-specified objects XML data Transport protocol RMI-IIOP binary or OAP GIOP/IIOP HTTP, HTTPS, SMTP, FTP Message exchange format Java object serialization.net object serialization ORB/CDR SOAP 12
Web services related.net namespaces System.Web.Services for developing Web services (e.g.: WebService, WebMethod) System.Web.Services.Configuration for extending SOAP System.Web.Services.Description for creating and manipulating WSDL descriptions System.Web.Services.Discovery for using DISCO System.Web.Services.Protocols for implementation of communication protocols (e.g. SOAP- HTTP) System.Xml.Serialization for XML serialization 13
Example 14
A.NET web service has a.asmx file associated with it. This is the equivalent of the.aspx file of ASP.net applications. 15
Running The URL of the web service The WSDL (web service description language) The operation (method) exposed by this web service 16
The name of the Web Service. The result of invoking method Hello World 17
The code: Service1.asmx.cs using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Web; using System.Web.Services; namespace WebService1 { /// <summary> /// Summary description for Service1. /// </summary> public class Service1 : System.Web.Services.WebService { public Service1() { //CODEGEN: This call is required by the ASP.NET Web Services Designer InitializeComponent(); } #region Component Designer generated code //Required by the Web Services Designer private IContainer components = null; Class Service1 is our Web Service. (Subclass of class WebService) 18
/// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if(disposing && components!= null) { components.dispose(); } base.dispose(disposing); } C# attribute, that makes this method exposed as part of a web service. / #endregion // WEB SERVICE EXAMPLE // The HelloWorld() example service returns the string Hello World } } [WebMethod] public string HelloWorld() { return "Hello World"; } The web service method HelloWord() 19
How to use the HelloWorld web service Any.NET application Console application Windows application Web Application can use a Web Service. 20
A console application that uses the HelloWorld Web Service Output string Hello World comes from the returned value of method HelloWorld() that had been invoked. 21
The code 1. Create a console application, and 2. add access to the existing web service 22
3. Find the Web Service to use. In this case, select from the local machine. 23
4. Once the service is found, type the name you like to use to reference it (HelloWorldWebService, in this example). 24
5. The web service is made available for use in ConsoleApplication1. 25
The method HelloWorld() of this web service. Class1 is the console application that uses (consumes) the web service. The constructor of the web service points to the location of the.asmx file, so that the web service is found when needs to be used. 26
The consumer class: Class1 Instance variable of type HelloWorldWebService.Service1 27
Class1 is the consumer, i.e., contains the code that uses the web service. using System; Declare a WebService namespace ConsoleApplication1 { class Class1 { private HelloWorldWebService.Service1 myws; } } [STAThread] static void Main(string[] args) { new Class1().go(); } public void go() { myws = new HelloWorldWebService.Service1(); Console.WriteLine( myws.helloworld() ); } Call web service s method HelloWorld() Create web service object. 28