1 1 Web Services
Jan 12, 2016
11
Web Services
2 2
Objectives
You will be able to Say what a web service is. Write and deploy a simple web
service. Test a simple web service. Write a client program for a simple
web service.
3 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 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
Visual Studio 2010 supports both methods. We will follow the traditional approach.
5 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. SOAP Simple Object Access 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 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 7
How do RMI Web Services Work?
The interface seen by the client program is the same as if the method were local.
But don't be deceived! There is a round trip message exchange before
the remote function returns. MUCH slower than a local function.
Good web services do a lot of work on a single method invocation.
8 8
How do web services work?
When using ASP.NET and Visual Studio Library functions handle most of the work. It is not necessary to understand the SOAP
protocol or message coding.
Let’s try a simple example: Hello Service A service that returns the message “Hello,
world!” whenever it is invoked.
9 9
Creating a Web Service
In Visual Studio 2010, create a new web site using the Web Service template. Note: New web site, not new project. Location: File System Language: C# Template: ASP.NET Web Service
10 10
New Web Site
11
Creating a Web Service
In the New Web Site box Select .NET Framework 3.5
NOT 4.0 Installed Templates: Visual C# ASP.NET Web Service Name: Hello_Service
12 12
Creating a Web Service
13 13
Creating a Web Service
Visual Studio creates Web site with the name Hello_Service. The name specified for the web site
Containing folders App_Code and App_Data Initial source code file called Service.asmx
14 14
The “Service”
Will be visible on the Internet when deployed to a Microsoft web server. Processed by the Microsoft Web Server (IIS) like .aspx files.
15 15
Initial Code for Our Web Service
A very simple example provided by Visual Studio.
16 16
Initial Code for Our Web Service
Note the class name, “Service”. Also the name of the .asmx file
Can be any name we wish. “Service” is just the default.
Let’s change the class name to “Hello_Service”
17
Create New Web Site
Change file names from Service to Hello_Service Hello_Service.asmx App_Code\Hello_Service.cs
In Hello_Service.cs Change name of class from Service to
Hello_Service Also constructor
18 18
Rename the .asmx File
Update CodeBehind file name and Class name.
19 19
Set Port Number
In Solution Explorer Select Project
Main Menu > View > Properties Window
20 20
Set Port Number
21 21
Set Port Number
22 22
Set Port Number
In the Properties Window Set Use dynamic ports to false. Set Port number to 4500
An arbitrary choice
23 23
Set Port Number
24 24
Class “Hello_Service"
Examine class Hello_Service
Derived from System.Web.Services.WebService
Note attributes (in square brackets) Compiler directives Method HelloWorld has attribute [WebMethod]
Any method to be provided to remote clients must have attribute [WebMethod]
25 25
Attributes of Class “Hello_Service"
[WebServiceBinding(...) ] Identifies the level of web services
interoperability that this web service will conform to.
Leave at default setting
26 26
Attributes of Class "Service"
[WebService( Namespace = ...) ] specifies a namespace to uniquely identify the web service.
Set your Namespace to your URL For example, in my case,
http://cis4930wp.eng.usf.edu/wpusr40
This is just an easy way to define a name that will be unique over the entire Internet.
There is no significance to the specific choice.
27 27
Hello_Service Class Definition
28 28
HelloWorld()
Normally you would delete the HelloWorld() method and enter your own methods in its place.
For this first example, we will leave it. Define a web method called
HelloWorld() which will return the string “Hello World” to any client that invokes the service.
29 29
Build the Service
Now build the service
Main Menu > Build > Build Web Site
30 30
Testing the Service
How do we test the service? It needs a client!
Browsers provide a default client that we can use to test a service running on Visual Studio. Right click on Hello_Service.asmx in the
Solution Explorer window. Select View in Browser.
31 31
Testing the Service
32 32
Testing the Service in Chrome
Click here
33 33
Testing the Service in Chrome
Click here
34 34
Here is the Result
Click the first tab to return, then click the “Back” button.
35 35
Service Description
Click here
36 36
Service Description - WSDL
End of Section
37 37
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.
Click the browser's back button to return to the web service page and leave it running.
38 38
Consuming a Web Service
Now let’s write a client app to use the HelloWorld web service. Leave this web service running.
Clients can be either PC programs or web apps Including other web services.
Since this is a web apps class, let's write a web app to use the Hello_Service.
39 39
Consuming a Web Service
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.
40 40
Consuming a Web Service
Start up another instance of Visual Studio 2010.
First instance must continue running to support the service.
Create a new C# ASPX.NET Web Site Call it Test_Hello.
41
Creating a Client App
42 42
Creating a Client App
43
Creating a Client App
Let the test app have a button to invoke the HelloService web method.
A label to display the result.
44 44
Design the Form
45 45
Add Reference to the Web Service
46
Get URL of the Running Web Service
Select and copy URL from address box
47
Paste in URL for the web service. (Or type it.) Then click .
47
Adding a Web Reference
Browsing won’t work when the service is running in Visual Studio’s built in web server.
48 48
Adding a Web Reference
49 49
Adding a Web Reference
Fill in a Web reference name. Than click “Add Reference”.
The name is your choice, but should be meaningful.
50
Adding a Web Reference
Visual Studio contacts the web site that we identified. Doesn't have to be on the same computer as
it is in this case. Could be anywhere.
Visual Studio gets the WSDL Web Service Specification from the web service.
Using the WSDL spec, Visual Studio creates the proxy and makes it a part of our website.
51 51
Check the Solution Explorer
52 52
Recall the Service Definition
The class that provides the HelloWorld remote method is called “Hello_Service”.
HelloWorld is a method of class “Hello_Service”.
53
Using the Web Service
We have to instantiate the Hello_Service class.
Then invoke its HelloWorld method.
54 54
Open Code Window in Test Program
55 55
Include the Web Reference Name in "Usings"
At the top, add using Hello_Service_on_localhost;
The web reference that we added to the application
This is the namespace in which the proxy class exists.
56
Default.aspx.cs with new “using”
57 57
Add an Event Handler
Back in Design window
Double click on the "Invoke HelloWorld" button, to generate an event handler for it.
Fill in code as shown on the next slide
58 58
Event Handler
private void btnInvokeHello_Click(object sender, EventArgs e)
{
Hello_Service hs = new Hello_Service();
try
{
lblResult.Text = hs.HelloWorld();
}
catch (Exception ex)
{
lblResult.Text = ex.Message;
}
}
The proxy class generated when we added the web reference
The specific method to invoke within the web service.
59 59
Try it!
Build and run the test app.
60 60
Here is our result.
61 61
Summary: Creating a Web Service Client
Know the URL and Interface definition for the web service.
Create a normal web application (or PC application).
Add a web reference. 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