1 web services visual c# 2008 step by step chapter 30

39
1 Web Services Visual C# 2008 Step by Step Chapter 30

Upload: gary-roberts

Post on 23-Dec-2015

224 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 Web Services Visual C# 2008 Step by Step Chapter 30

1

Web Services

Visual C# 2008 Step by StepChapter 30

Page 2: 1 Web Services Visual C# 2008 Step by Step Chapter 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.

Page 3: 1 Web Services Visual C# 2008 Step by Step Chapter 30

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: 1 Web Services Visual C# 2008 Step by Step Chapter 30

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.

Page 5: 1 Web Services Visual C# 2008 Step by Step Chapter 30

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.

Page 6: 1 Web Services Visual C# 2008 Step by Step Chapter 30

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: 1 Web Services Visual C# 2008 Step by Step Chapter 30

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

Page 8: 1 Web Services Visual C# 2008 Step by Step Chapter 30

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

Page 9: 1 Web Services Visual C# 2008 Step by Step Chapter 30

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;

}

}

Page 10: 1 Web Services Visual C# 2008 Step by Step Chapter 30

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.

Page 11: 1 Web Services Visual C# 2008 Step by Step Chapter 30

11

Testing the Service

Page 12: 1 Web Services Visual C# 2008 Step by Step Chapter 30

12

Testing the Service

Click here

Page 13: 1 Web Services Visual C# 2008 Step by Step Chapter 30

13

Testing the Service

Click here

Page 14: 1 Web Services Visual C# 2008 Step by Step Chapter 30

14

Here is the Result

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

Page 15: 1 Web Services Visual C# 2008 Step by Step Chapter 30

15

Service Description

Click here

Page 16: 1 Web Services Visual C# 2008 Step by Step Chapter 30

16

Service Description - WSDL

End of Section

Page 17: 1 Web Services Visual C# 2008 Step by Step Chapter 30

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.

Page 18: 1 Web Services Visual C# 2008 Step by Step Chapter 30

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.

Page 19: 1 Web Services Visual C# 2008 Step by Step Chapter 30

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

Page 20: 1 Web Services Visual C# 2008 Step by Step Chapter 30

20

Creating a Client Program

Page 21: 1 Web Services Visual C# 2008 Step by Step Chapter 30

21

Design the Form

btnSayHello

tbResult

tbName

Page 22: 1 Web Services Visual C# 2008 Step by Step Chapter 30

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

Page 23: 1 Web Services Visual C# 2008 Step by Step Chapter 30

23

Add Service Reference

Page 24: 1 Web Services Visual C# 2008 Step by Step Chapter 30

24

Add Service Reference

Click here

Page 25: 1 Web Services Visual C# 2008 Step by Step Chapter 30

25

Make it a Web Reference

Click here

Page 26: 1 Web Services Visual C# 2008 Step by Step Chapter 30

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.

Page 27: 1 Web Services Visual C# 2008 Step by Step Chapter 30

27

Adding a Web Reference

Page 28: 1 Web Services Visual C# 2008 Step by Step Chapter 30

28

Adding a Web Reference

Fill in Web a reference name. Than click “Add Reference”.

Page 29: 1 Web Services Visual C# 2008 Step by Step Chapter 30

29

Check the Solution Explorer

Page 30: 1 Web Services Visual C# 2008 Step by Step Chapter 30

30

What Functions Does the Service Provide?

Page 31: 1 Web Services Visual C# 2008 Step by Step Chapter 30

31

Functions That We Can Call

Page 32: 1 Web Services Visual C# 2008 Step by Step Chapter 30

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

Page 33: 1 Web Services Visual C# 2008 Step by Step Chapter 30

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.

Page 34: 1 Web Services Visual C# 2008 Step by Step Chapter 30

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

Page 35: 1 Web Services Visual C# 2008 Step by Step Chapter 30

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.

Page 36: 1 Web Services Visual C# 2008 Step by Step Chapter 30

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.

Page 37: 1 Web Services Visual C# 2008 Step by Step Chapter 30

37

Try it!

Build and run the project.

Page 38: 1 Web Services Visual C# 2008 Step by Step Chapter 30

38

Here is our result.

Page 39: 1 Web Services Visual C# 2008 Step by Step Chapter 30

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