Top Banner
1 1 Web Services
61

11 Web Services. 22 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.

Jan 12, 2016

Download

Documents

Godfrey Clarke
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 11 Web Services. 22 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.

11

Web Services

Page 2: 11 Web Services. 22 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.

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.

Page 3: 11 Web Services. 22 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.

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

Page 4: 11 Web Services. 22 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.

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.

Page 5: 11 Web Services. 22 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.

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.

Page 6: 11 Web Services. 22 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.

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.

Page 7: 11 Web Services. 22 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.

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.

Page 8: 11 Web Services. 22 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.

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.

Page 9: 11 Web Services. 22 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.

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

Page 10: 11 Web Services. 22 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.

10 10

New Web Site

Page 11: 11 Web Services. 22 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.

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

Page 12: 11 Web Services. 22 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.

12 12

Creating a Web Service

Page 13: 11 Web Services. 22 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.

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

Page 14: 11 Web Services. 22 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.

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.

Page 15: 11 Web Services. 22 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.

15 15

Initial Code for Our Web Service

A very simple example provided by Visual Studio.

Page 16: 11 Web Services. 22 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.

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”

Page 17: 11 Web Services. 22 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.

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

Page 18: 11 Web Services. 22 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.

18 18

Rename the .asmx File

Update CodeBehind file name and Class name.

Page 19: 11 Web Services. 22 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.

19 19

Set Port Number

In Solution Explorer Select Project

Main Menu > View > Properties Window

Page 20: 11 Web Services. 22 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.

20 20

Set Port Number

Page 21: 11 Web Services. 22 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.

21 21

Set Port Number

Page 22: 11 Web Services. 22 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.

22 22

Set Port Number

In the Properties Window Set Use dynamic ports to false. Set Port number to 4500

An arbitrary choice

Page 23: 11 Web Services. 22 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.

23 23

Set Port Number

Page 24: 11 Web Services. 22 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.

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]

Page 25: 11 Web Services. 22 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.

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

Page 26: 11 Web Services. 22 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.

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.

Page 27: 11 Web Services. 22 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.

27 27

Hello_Service Class Definition

Page 28: 11 Web Services. 22 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.

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.

Page 29: 11 Web Services. 22 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.

29 29

Build the Service

Now build the service

Main Menu > Build > Build Web Site

Page 30: 11 Web Services. 22 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.

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.

Page 31: 11 Web Services. 22 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.

31 31

Testing the Service

Page 32: 11 Web Services. 22 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.

32 32

Testing the Service in Chrome

Click here

Page 33: 11 Web Services. 22 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.

33 33

Testing the Service in Chrome

Click here

Page 34: 11 Web Services. 22 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.

34 34

Here is the Result

Click the first tab to return, then click the “Back” button.

Page 35: 11 Web Services. 22 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.

35 35

Service Description

Click here

Page 36: 11 Web Services. 22 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.

36 36

Service Description - WSDL

End of Section

Page 37: 11 Web Services. 22 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.

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.

Page 38: 11 Web Services. 22 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.

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.

Page 39: 11 Web Services. 22 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.

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.

Page 40: 11 Web Services. 22 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.

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.

Page 41: 11 Web Services. 22 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.

41

Creating a Client App

Page 42: 11 Web Services. 22 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.

42 42

Creating a Client App

Page 43: 11 Web Services. 22 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.

43

Creating a Client App

Let the test app have a button to invoke the HelloService web method.

A label to display the result.

Page 44: 11 Web Services. 22 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.

44 44

Design the Form

Page 45: 11 Web Services. 22 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.

45 45

Add Reference to the Web Service

Page 46: 11 Web Services. 22 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.

46

Get URL of the Running Web Service

Select and copy URL from address box

Page 47: 11 Web Services. 22 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.

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.

Page 48: 11 Web Services. 22 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.

48 48

Adding a Web Reference

Page 49: 11 Web Services. 22 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.

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.

Page 50: 11 Web Services. 22 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.

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.

Page 51: 11 Web Services. 22 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.

51 51

Check the Solution Explorer

Page 52: 11 Web Services. 22 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.

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”.

Page 53: 11 Web Services. 22 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.

53

Using the Web Service

We have to instantiate the Hello_Service class.

Then invoke its HelloWorld method.

Page 54: 11 Web Services. 22 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.

54 54

Open Code Window in Test Program

Page 55: 11 Web Services. 22 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.

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.

Page 56: 11 Web Services. 22 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.

56

Default.aspx.cs with new “using”

Page 57: 11 Web Services. 22 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.

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

Page 58: 11 Web Services. 22 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.

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.

Page 59: 11 Web Services. 22 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.

59 59

Try it!

Build and run the test app.

Page 60: 11 Web Services. 22 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.

60 60

Here is our result.

Page 61: 11 Web Services. 22 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.

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