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
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.
Web Services vs “Local” Services …
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 service
Web service
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,
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 service
Web service
Evolution of the internet and the WWW (0)
Generation 0
FTP, e-mail
Evolution of the web (1)
Generation 1
Static HTML
Evolution of the web (2)
Generation 2
Web Applications
Evolution of the web (3)
HTML, XML
HTML, XML
Generation 3 (under construction)
Evolution of the web (4)
empID
HTML, XML
HTML, XML, RDF
Generation 4 (under contemplation)
Semantic Web
Emp-id
Check and be able to realize that empIDand Emp-id mean the same thing.
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).
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
Comparison of various distributed (Web Services and
ancestors) technologies
SOAP ORB/CDR .NET object serialization Java object serialization Message exchange format HTTP, HTTPS, SMTP, FTP GIOP/IIOP binary or OAP RMI-IIOP Transport 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 independent independent .NET languages (C#, VB.NET, ..) Java Programming languageWeb
services
CORBA
.NET
Remoting
Java RMI
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)
A .NET web service has a .asmx file associated with it. This is the equivalent of
the .aspx file of ASP.net applications.
Running …
The operation (method) exposed by this web service
The URL of the web service
The WSDL (web service description
The result of invoking method
“Hello World”
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
Class Service1 is our Web Service. (Subclass of
…/
/// <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
How to use the HelloWorld web service
• Any .NET application
– Console application
– Windows application
– Web Application
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.
The code
1. Create a console application, and
3. Find the Web Service to use. In this case,
select from the local machine.
4. Once the service is found, type the name you like to use
to reference it (HelloWorldWebService, in this example).
5. The web service is made available for use in
ConsoleApplication1.
The method HelloWorld() of this web service.
Class1 is the console application that uses (consumes) the web service.
The consumer class: Class1
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