Web Services

Download Report

Transcript Web Services

Web Services
Visual C# 2008 Step by Step
Chapter 30
1
Objectives
You will be able to
 Say what a web service is.
 Describe the interaction between a web
service and a client program.
 Write a client program for a simple web
service.
2
What are Web Services?

Make web apps available to programs

Like they are available to human users via
web browsers.

Use HTTP
SOAP
XML

Motivation:




Platform independence
Drawback

High communication overhead
3
Web Service Frameworks

Version 3.5 of the .NET Framework
provides two quite different ways to
develop web services:

ASP.NET Web Services


Windows Communications Foundation




The traditional approach
New in .NET 3.5 and Visual Studio 2008
Covered in our textbook
Visual Studio 2008 supports both methods.
We will follow the traditional approach.
4
What is a .NET Web Service?

Web Services in the .NET environment



Motivation:




A form of remote method invocation (RMI)
Layered on top of basic message based web service
protocol.
Method invocation is familiar to programmers.
No need to understand the SOAP protocol.
No need to work directly with messages.
This is the only form of web service that we will study.
5
How do RMI Web Services Work?






Client code invokes “proxy” method on own system.
 Same interface as the remote method
Proxy prepares a SOAP message encoding identification
of function and arguments.
Proxy sends message to web server on remote system.
Web server on remote system parses the SOAP message
and invokes the specified method.
Web server sends result back as a SOAP message.
Proxy on client system parses message and delivers
result.
6
How do web services work?

When using ASP.NET and Visual Studio



Libarary functions handle most of the work.
It is not necessary to understand the SOAP
protocol or XML in order to use a web service.
Let’s try a simple example:


Greeting Service
A web service method that returns the message
“Hello, <client name>” whenever it is invoked with a
client’s name as the parameter value..
7
The Web Service




Download from the class web site:
http://www.cse.usf.edu/~turnerr/Software_Systems_Development/
Downloads/2011_04_19_Web_Services/ File Greeting_Service.zip
Expand the file.
In Visual Studio




File > Open Web Site
Select Greeting_Service (Second level down)
Click “Open”
Examine the code

Greeting_Service.cs
8
Greeting_Service.cs
using
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Web;
System.Web.Services;
[WebService(Namespace = "http://rollinsturner.net")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET
AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Greeting_Service : System.Web.Services.WebService
{
public Greeting_Service () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string Say_Hello(string name) {
return "Hello, " + name;
}
}
9
Testing the Service

How do we test the service?


It needs a client!
Internet Explorer provides a client that
we can use to test a web service running
under Visual Studio.


Right click on Greeting_Service.asmx in the
Solution Explorer window.
Select View in Browser.
10
Testing the Service
11
Testing the Service
Click here
12
Testing the Service
Click here
13
Here is the Result
Click the first tab to return, then click
the “Back” button.
14
Service Description
Click here
15
Service Description - WSDL
End of Section
16
WSDL




Web Services Description Language
A web service must be able to provide a detailed
specification of its interface upon request.
Permits the client to construct a SOAP message in the
correct format for this service.
Visual Studio handles this transparently when we use it
to write client software.
17
Consuming a Web Service

Now let’s write a program to use the
Greeting Service.


Leave the web service running.
We will need a proxy



Presents same appearance to the client
program as the remote method
Communicates with the web server using
SOAP messages.
Passes result to client program as if the
method had been executed locally.
18
Consuming a Web Service

Start up another instance of Visual Studio 2008.


First instance must continue running to support the
service.
Create a new C# Windows Forms Application
project.


Project. Not web site!
Call it Test_Greeting_Service
19
Creating a Client Program
20
Design the Form
tbName
btnSayHello
tbResult
21
Add Reference to the Web Service

Adding a reference to the web service
generates code for a class that will be the
local proxy for the remote service.



Provides the same interface that the web
service defines at the remote site.
Name of the proxy class will be the same as
the name of the class that provides the web
service.
Project > Add Service Reference
22
Add Service Reference
23
Add Service Reference
Click
here
24
Make it a Web Reference
Click
here
25
Adding a Web Reference
Paste in URL for the web service. (Or type it.)
Then click Go.
Browsing won’t work when the service is running in Visual
Studio’s built in web server.
26
Adding a Web Reference
27
Adding a Web Reference
Fill in Web a reference name. Than click “Add Reference”.
28
Check the Solution Explorer
29
What Functions Does the Service Provide?
30
Functions That We Can Call
31
Recall the Service Definition
The class that provides the
Say_Hello remote method was
called “Greeting_Service”.
Say_Hello is a method of
class “Greeting_Service”.
32
Recall the Service Definition


The proxy, generated by Visual Studio
when we added the web reference,
makes a functionally identical class
available in the client program.
Looks and acts as if the web service
were local to the client program.
33
Include the Web Reference in "Usings"

Open the code window for Form1.cs

At the top, add:
using Test_Greeting_Service.Greeting_Service_on_localhost;
Namespace of this application
The web reference that we
added to the application
34
Add an Event Handler
Back in Design window


Double click on the “Say Hello" button,
to generate an event handler for it.
Fill in code to instantiate the
Greeting_Service class and invoke its
Say_Hello() method.
35
Event Handler
using System;
using System.Windows.Forms;
using Test_Greeting_Service.Greeting_Service_on_localhost;
namespace Test_Greeting_Service
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnSayHello_Click(object sender, EventArgs e)
{
The proxy class
Greeting_Service gs = new Greeting_Service(); generated when we
added the web
reference
tbResult.Text = gs.Say_Hello(tbName.Text);
}
}
}
The specific method to invoke
within the web service.
36
Try it!

Build and run the project.
37
Here is our result.
38
Summary: Creating a Web Service Client



Know the URL and Interface definition for the web service.
Create a normal Windows Forms application.
Add a web reference.

Start with service reference. Click “Advanced”.
Specify URL of the web service.
 This adds a proxy class to the project.
 Proxy class interface is identical to that of the web service.
Add “using” statement for the web reference.
Instantiate the web service class.
Invoke methods of the web service as if it were a local class.




End of Presentation
39