Upload
ngobao
View
213
Download
0
Embed Size (px)
Citation preview
1
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.
2
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( …) { …}
3
Web Services vs “Local” Services …
System calls are written in some language (part of the OS).
SOAP is written in XML.
Access is done via local system calls (managed by the local operating system).
Access is done via “remote system calls” managed by HTTP and SOAP (Simple Object Access Protocol) a protocol that allows calling remote methods).
Class produces DLL file which is stored in local computer, and it is accessed from classes residing in same computer.
Class produces DLL file which is stored in local computer, and it is accessed from classes residing in some other computer
Local serviceWeb service
4
Web Services vs “Local” Services …/
We learn about local methods via the API.
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 ]
API may be written in XML.WSDL is written in XML.
Documentation for a local method is provided via a local API(Application Programming Interface).
Documentation for a web method is provided via WSDL(Web Service Description Language).
Local serviceWeb service
9
Evolution of the web (4)
empIDHTML, XML
HTML, XML, RDF
Generation 4 (under contemplation)Semantic Web
Emp-idCheck and be able to realize that empID
and Emp-id mean the same thing.
10
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).
11
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)
12
Comparison of various distributed (Web Services and ancestors) technologies
SOAPORB/CDR.NET object serialization
Java object serialization
Message exchange format
HTTP, HTTPS, SMTP, FTP
GIOP/IIOPbinary or OAPRMI-IIOPTransport protocol
XML data IDL-specified objects
.NET objects Java objects (binary)
Data exchange format
WSDL (XML-based)
CORBA IDL.NET Interfaces Java Interfaces Interface definition
independentindependent.NET languages (C#, VB.NET, ..)
Java Programming language
Web services
CORBA.NET Remoting
Java RMI
13
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
15
A .NET web service has a .asmx file associated with it. This is the equivalent of the .aspx file of ASP.net
applications.
16
Running …
The operation (method) exposed by this web service
The URL of the web service
The WSDL (web service description
language)
18
The code: Service1.asmx.csusing 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)
19
…//// <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);
}
#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()
C# attribute, that makes this method exposed as part of a web service.
20
How to use the HelloWorld web service
• Any .NET application
– Console application– Windows application– Web Application
can use a Web Service.
21
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.
24
4. Once the service is found, type the name you like to use to reference it (HelloWorldWebService, in this example).
26
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.
The method HelloWorld() of this web service.
Class1 is the console application that uses (consumes) the web service.
28
Class1 is the consumer, i.e., contains the code that uses the web service.
using System;
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() );
}}
}
Declare a WebService
Create web service object.
Call web service’s method HelloWorld()