1 web services visual c# 2008 step by step chapter 30
TRANSCRIPT
1
Web Services
Visual C# 2008 Step by StepChapter 30
2
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.
3
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
4
Web Service Frameworks
Version 3.5 of the .NET Framework provides two quite different ways to develop web services:
ASP.NET Web Services The traditional approach
Windows Communications Foundation 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.
5
What is a .NET Web Service?
Web Services in the .NET environment A form of remote method invocation (RMI) Layered on top of basic message based web
service protocol.
Motivation: 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.
6
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.
7
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..
8
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
9
Greeting_Service.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using 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;
}
}
10
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.
11
Testing the Service
12
Testing the Service
Click here
13
Testing the Service
Click here
14
Here is the Result
Click the first tab to return, then click the “Back” button.
15
Service Description
Click here
16
Service Description - WSDL
End of Section
17
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.
18
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.
19
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
20
Creating a Client Program
21
Design the Form
btnSayHello
tbResult
tbName
22
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
23
Add Service Reference
24
Add Service Reference
Click here
25
Make it a Web Reference
Click here
26
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.
27
Adding a Web Reference
28
Adding a Web Reference
Fill in Web a reference name. Than click “Add Reference”.
29
Check the Solution Explorer
30
What Functions Does the Service Provide?
31
Functions That We Can Call
32
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”.
33
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.
34
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
35
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.
36
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)
{
Greeting_Service gs = new Greeting_Service();
tbResult.Text = gs.Say_Hello(tbName.Text);
}
}
}
The proxy class generated when we added the web reference
The specific method to invoke within the web service.
37
Try it!
Build and run the project.
38
Here is our result.
39
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