visual basic.net asp.net web services february 25, 2004

74
Visual Basic.NET ASP.NET Web Services February 25, 2004

Upload: lesley-avila

Post on 03-Jan-2016

23 views

Category:

Documents


2 download

DESCRIPTION

Visual Basic.NET ASP.NET Web Services February 25, 2004. Agenda – February 25, 2004. Homework or Other Questions? (Cookies?) Evolution of Visual Basic.NET Programming ASP.NET Web Services HelloService Class Exercise What is SOAP? Web Service Directive - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Visual Basic.NET ASP.NET Web Services February 25, 2004

Visual Basic.NET

ASP.NET Web Services

February 25, 2004

Page 2: Visual Basic.NET ASP.NET Web Services February 25, 2004

Agenda – February 25, 2004

• Homework or Other Questions? (Cookies?)• Evolution of Visual Basic.NET Programming• ASP.NET Web Services• HelloService Class Exercise• What is SOAP?• Web Service Directive• WSDL (Web Services Description Language)• Web Services Presentation• Web Service Class Exercise• Final Homework / Class Project

Page 3: Visual Basic.NET ASP.NET Web Services February 25, 2004

Homework or Other Questions?

Page 4: Visual Basic.NET ASP.NET Web Services February 25, 2004
Page 5: Visual Basic.NET ASP.NET Web Services February 25, 2004

Evolution of Visual Basic.NET Programming

Page 6: Visual Basic.NET ASP.NET Web Services February 25, 2004

Console Application

Command Window

Console Application

Page 7: Visual Basic.NET ASP.NET Web Services February 25, 2004

Windows Form Class

Input Screen

Windows Form Application

Page 8: Visual Basic.NET ASP.NET Web Services February 25, 2004

Constructors

Fields

Get Set Properties

Methods

Windows Form Class

Class

Input Screen

Windows Form with Class DLL

Page 9: Visual Basic.NET ASP.NET Web Services February 25, 2004

Database

Constructors

Fields

Get Set Properties

MethodsWindows Form

Class

ClassInput Screen

Windows Form, Class DLL, and ADO

Page 10: Visual Basic.NET ASP.NET Web Services February 25, 2004

WebForm Class

Browser (IE6)

WebForm Application

Code Behind File

Page 11: Visual Basic.NET ASP.NET Web Services February 25, 2004

Database

Constructors

Fields

Get Set Properties

Methods

WebForm Class

ClassBrowser (IE6)

WebForm, Class DLL, and ADO

Code Behind File

Page 12: Visual Basic.NET ASP.NET Web Services February 25, 2004

Database

Constructors

Fields

Get Set Properties

Methods

WebForm Class

ClassBrowser (IE6)

Web Services using localhost

Code Behind File

SOAP

Page 13: Visual Basic.NET ASP.NET Web Services February 25, 2004

Database

Constructors

Fields

Get Set Properties

Methods

WebForm Class

ClassBrowser (IE6)

Web Services using Remote Host

Code Behind File

Client Server

SOAP

Page 14: Visual Basic.NET ASP.NET Web Services February 25, 2004
Page 15: Visual Basic.NET ASP.NET Web Services February 25, 2004

ASP.NET Web Services

Page 16: Visual Basic.NET ASP.NET Web Services February 25, 2004

Web Services

• Web Services are a new way of performing remote procedure calls over HTTP

• Web Services make use of SOAP (Simple Object Access Protocol). SOAP is an XML-based standard that details how method calls are made over HTTP in a reproducible manner

• Web Services are completely described using WSDL (Web Service Description Language)

• Web Services are able to use the full array of Visual Basic, C# and .NET techniques over the server.

Page 17: Visual Basic.NET ASP.NET Web Services February 25, 2004

Web Services.• A web service is an application that exposes a Web-

accessible API (i.e. a class method).– The key is that it is Web-accessible, not just browser-

accessible.• Another way of looking at a .NET web service is that it

is a unit of managed code, typically installed under IIS, that can be remotely invoked using HTTP.

• A Web service is language agnostic.– All you care about is invoking the service with the

input parameters formatted correctly, and parsing the output (if any).

Page 18: Visual Basic.NET ASP.NET Web Services February 25, 2004

Web Services Architecture

Windows Client

Browser Client

Other Client

Web Server (IIS)

Request Handler

Web Service

Web Service

SOAP Messages

Server

Page 19: Visual Basic.NET ASP.NET Web Services February 25, 2004

Anatomy of a Web Service

• In .NET, a web service is implemented by one or more assemblies, just like any other library.

• In addition, some additional infrastructure is needed:– A wire protocol – used to transmit your service

request and receive a response. (i.e. HTTP/GET, HTTP/POST or SOAP).

– A description service – so clients can programmatically access and examine the interface contract.

– A discovery service – so clients can programmatically discover the existence of web services.

Page 20: Visual Basic.NET ASP.NET Web Services February 25, 2004

Web Service Security• Project Files are stored in assemblies

• Support for strongly named assemblies

• Stored in GAC (Global Assembly Cache) found in C:\WINDOWS\assembly

• Using Public / Private Key Pairs (Kerberos)– Assembly Signed with the Private Key– Assembly is Opened with the Public Key– Guaranteed to be the correct Assembly

Page 21: Visual Basic.NET ASP.NET Web Services February 25, 2004
Page 22: Visual Basic.NET ASP.NET Web Services February 25, 2004

HelloService Class Exercise

Page 23: Visual Basic.NET ASP.NET Web Services February 25, 2004

Let's Look at Converting this Simple Code into a Web Service

Imports System

Class Hello

Public Function HelloMessage() As String

Return ("Hello .NET Web Service")

End Function

End Class

Page 24: Visual Basic.NET ASP.NET Web Services February 25, 2004

The HelloService.asmx File<%@ WebService Language="VB" Class="HelloService" %>

'Imports System'Imports System.Web.Services

Public Class HelloService Inherits System.Web.Services.WebService

<System.Web.Services.WebMethod()> _Public Function HelloMessage() As String

Return ("Hello .NET Web Service")End Function

End Class

Page 25: Visual Basic.NET ASP.NET Web Services February 25, 2004

Steps to Implement

1. Save the above text in a file named hello.asmx file in the C:\Inetpub\wwwroot directory

2. Access the web service by specifying the .asmx file in the URL

(i.e. http://localhost/simplehello.asmx)

3. Invoking the web service this way automatically generates a web page that specifies the message format, and gives you a convenient invocation button for simple parameters. (i.e. it only supports a GET)

4. Notice that the response is wrapped in XML

Page 26: Visual Basic.NET ASP.NET Web Services February 25, 2004
Page 27: Visual Basic.NET ASP.NET Web Services February 25, 2004

SOAP(Simple Object Access Protocol)

Page 28: Visual Basic.NET ASP.NET Web Services February 25, 2004

SOAP(Simple Object Access Protocol)

Remote objects can give a program almost unlimited power over the Internet, but most

firewalls block non-HTTP requests. SOAP, an XML-based protocol, gets around this limitation to provide intraprocess communication across

machines.

Page 29: Visual Basic.NET ASP.NET Web Services February 25, 2004

SOAP(Simple Object Access Protocol)

• Traditionally, HTTP is the application level protocol used between web-clients and web servers.

• Web clients are now more than browsers requesting HTML.

• HTTP is fairly simple, restricted to requesting and sending resources with GET, PUT and POST, but is not designed for flexible exchange of data between applications.

• The Simple Object Access Protocol (SOAP) aims to fix this by defining a protocol that any application can use to communicate and exchange data with any other application

Page 30: Visual Basic.NET ASP.NET Web Services February 25, 2004

SOAP(Simple Object Access Protocol)

• SOAP is a simple, lightweight XML-based protocol for exchanging structured and type information on the Web.

• SOAP uses XML Grammar tailored for exchanging web service data

• .NET Web Service usually sends SOAP messages over HTTP

• SOAP Protocol was originally developed by Compaq, HP, IBM, Lotus, Microsoft, and others

Page 31: Visual Basic.NET ASP.NET Web Services February 25, 2004

SOAP(Simple Object Access Protocol)

• To achieve maximum flexibility, SOAP uses XML syntax to format its content.

• SOAP is designed to be:– As simple as possible– Provide a minimum functionality

• Therefore SOAP is both modular and flexible.• However, the SOAP protocol does not include anything

on security or transactions

Page 32: Visual Basic.NET ASP.NET Web Services February 25, 2004

SOAP(Simple Object Access Protocol)

• Since SOAP is an application level protocol, it can run on any transport e.g. TCP.

• The protocol defines a messaging framework that contains no application or transport semantics. As a result, the protocol is modular and very extensible.

• Since SOAP messages are XML, which is plain text, they can easily pass through firewalls

• SOAP is not limited to name/value pairs as HTTP-GET and HTTP-POST are, but can send complex objects including datasets, classes, and other objects.

• SOAP focuses on the ability to send messages from a sender to a recipient. The use of SOAP as an RPC mechanism is purely a byproduct of this functionality. RPC-like behavior is achieved by sending messages in both directions.

Page 33: Visual Basic.NET ASP.NET Web Services February 25, 2004

SOAP(Simple Object Access Protocol)

• One negative to using SOAP is that messages tend to be verbose, because of the nature of XML.

• If bandwidth or transmission performance is an issue, HTTP-GET or HTTP-POST maybe better choices.

• .NET Frameworks provides a class, the SoapHttpClientProtocol for using the SOAP protocol in your clients.

• .NET Frameworks provides a number of classes for interacting with HTTP protocol, including a hierarchy of classes for SOAP, HTTP-GET and HTTP-POST client protocols, all deriving from WebClientProtocol and HttpWebClientProtocol.

Page 34: Visual Basic.NET ASP.NET Web Services February 25, 2004

Structure of a SOAP Message

SOAP Envelope

SOAP Header

SOAP Body

Header parts

Payload

SOAP Fault

Page 35: Visual Basic.NET ASP.NET Web Services February 25, 2004

Structure of a SOAP message• The Envelope is the top-level container

representing the message. • The Header is a generic container for added

features to a SOAP message in a decentralized manner. SOAP defines attributes to indicate who should deal with a feature and whether understanding is optional or mandatory.

• The Body is a container for mandatory information intended for the ultimate message receiver. SOAP defines one element for the body to report errors, called a SOAP Fault.

Page 36: Visual Basic.NET ASP.NET Web Services February 25, 2004

SOAP styles

• SOAP can have 2 styles – Document-style SOAP, which is used to simply

exchange XML data.– RPC-style SOAP, which is used to make an RPC

request and get it’s response.• Both styles of SOAP messages look the same, but the

body of an RPC style SOAP message has some rules around how requests/responses are tagged.

Page 37: Visual Basic.NET ASP.NET Web Services February 25, 2004

A Basic SOAP Message

<SOAPENV:Envelope xmlns:SOAPENV="http://schemas.xmlsoap.org/soap/envelope/"/>

   <SOAPENV:Body>        <StockQuoteRequest symbol="FOO"/>   </SOAPENV:Body>

</SOAPENV:Envelope>

• This message has just an envelope and a body.• The SOAP envelope namespace has to be specified to make

this a valid SOAP message.• This shows a document-style SOAP message.

Page 38: Visual Basic.NET ASP.NET Web Services February 25, 2004

SOAP Faults and .NET exceptions

• Any .NET exception that is thrown on the web service side is automatically wrapped in a SOAPException, so it can be serialized as a SOAP Fault.

• The InnerException chain is lost.• One way to transmit meaningful information is to catch

exceptions at the Web Service, and re-throw a SOAPException, with additional information stuck into the Details element of the SOAPException class.

• The receiving side can parse out the SOAP fault detail.

Page 39: Visual Basic.NET ASP.NET Web Services February 25, 2004

SOAP Specification

http://www.w3.org/TR/soap

Page 40: Visual Basic.NET ASP.NET Web Services February 25, 2004
Page 41: Visual Basic.NET ASP.NET Web Services February 25, 2004

WSDL (Web Services Description Language)

Page 42: Visual Basic.NET ASP.NET Web Services February 25, 2004

WSDL (Web Services Description Language)

• The XML Web services infrastructure is founded on communication via XML-based messages that comply with a published service description.

• The service description is an XML document written in an XML grammar called WSDL (Web Services Description Language) that defines the format of messages the XML Web service understands.

• The service description serves as an agreement that defines the behavior of an XML Web service and instructs potential clients in how to interact with it.

• The service description also specifies what the service consumer can expect to happen when a properly formatted message is submitted to the XML Web service.

Page 43: Visual Basic.NET ASP.NET Web Services February 25, 2004

WSDL File Document Elements• A Web Service is described with a WSDL File. The following

elements are involved in a WSDL document.– definitions – root element of a WSDL document.

– types – optional element can be used to define the data types used to describe the messages exchanged by this service.

– message – describes the messages exchanged by this service. Elements that represent the request and response, one per protocol (SOAP, HTTP GET and HTTP POST).

– portType – defines a set of abstract operations (i.e. a single round-trip query and response), giving it a name.

– binding – connects an abstract message to its message and transport (i.e. SOAP, HTTP GET, HTTP POST). Generally one binding element per transport.

– service – maps each portType to its binding, associates a port and binding to a location (URL) where the service can be reached.

– documentation – contains additional human readable information about the service.

– import – allows a WSDL document to include the contents of another.

Page 44: Visual Basic.NET ASP.NET Web Services February 25, 2004

WSDL in Visual Studio.NET

In Visual Studio.NET, for most part, you can ignore the details of the WSDL file and

concentrate on implementing the service.

Page 45: Visual Basic.NET ASP.NET Web Services February 25, 2004

WSDL Specification

http://www.w3.org/TR/wsdl

Page 46: Visual Basic.NET ASP.NET Web Services February 25, 2004
Page 47: Visual Basic.NET ASP.NET Web Services February 25, 2004

Web Service Directive

Page 48: Visual Basic.NET ASP.NET Web Services February 25, 2004

Web Service Directive

Required for all web services• Directive Name WebService• Variable number of attribute/value pairs

– Language (C#, VB, JS, VJ#, ...) C# is default– Class (required, class in either the .asmx or code-

behind file, the code-behind file must be compliled and stored in the ./bin subdirectory)

<%@ WebService Language="VB" Class="Hello" %>

Page 49: Visual Basic.NET ASP.NET Web Services February 25, 2004

WebService Attribute

• Optional

• Provides additional information to be added to the Web Service

• Properties:

– Description Property

– Name Property

– Namespace Property

Page 50: Visual Basic.NET ASP.NET Web Services February 25, 2004

WebMethod Attribute

Imports System.Web.Services

Public Class HelloService

Inherits System.Web.Services.WebService

<WebMethod(BufferResponse:=False)> _

Public Function HelloMessage() As String

• the Method must be public

• the WebMethod attribute has a number of properties

Page 51: Visual Basic.NET ASP.NET Web Services February 25, 2004

WebMethod Attribute Properties

<WebMethod(property="value" ...)>

• BufferResponse Property

• CacheDuration Property

• Description Property

• EnableSession

• MessageName

• TransactionOption

Page 52: Visual Basic.NET ASP.NET Web Services February 25, 2004

Discovery File

http://localhost/helloservice.asmx?WSDL

• Provides a way for developers to find out about the methods exposed by the web service.

• Written in WSDL (Web Service Description Language)

Page 53: Visual Basic.NET ASP.NET Web Services February 25, 2004

Creating a Discovery File

In a command window:

disco.exe /out:<output directory name> http://localhost/simplehello.asmx

Creates three output files– .disco file– .WSDL file– results.discomap file

Page 54: Visual Basic.NET ASP.NET Web Services February 25, 2004

Sample .disco File

<?xml version="1.0" encoding="utf-8"?>

<discovery

xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.xmlsoap.org/disco/">

<contractRef ref=http://localhost/SimpleHello.asmx?wsdl docRef=http://localhost/SimpleHello.asmx xmlns="http://schemas.xmlsoap.org/disco/scl/" />

<soap address=http://localhost/SimpleHello.asmx xmlns:q1=http://tempuri.org/ binding="q1:HelloSoap" xmlns="http://schemas.xmlsoap.org/disco/soap/" />

</discovery>

Page 55: Visual Basic.NET ASP.NET Web Services February 25, 2004
Page 56: Visual Basic.NET ASP.NET Web Services February 25, 2004

Web Service Deployment

• .asmx File (and .disco File) must reside in a virtual directory exposed by IIS so that they are accessible to a browser on the Internet.

• Directory structure is the same as for a .NET Web Form application with the code-behind file, web.config file (if it exists), ./bin/WebServiceName.dll, etc.)

Page 57: Visual Basic.NET ASP.NET Web Services February 25, 2004
Page 58: Visual Basic.NET ASP.NET Web Services February 25, 2004

Advanced Hello Web ServiceClass Exercise

Page 59: Visual Basic.NET ASP.NET Web Services February 25, 2004

Let’s write a web service with Visual Studio.NET

• Create a new project of type “ASP.NET Web Service”• Notice that the project automatically creates a new

virtual directory under IIS. (You may specify an existing virtual directory if you want.)

• A number of files are created, but the important ones are:– The .asmx file– The .asmx.vb file– The web.config file

Page 60: Visual Basic.NET ASP.NET Web Services February 25, 2004

The .asmx file

• The .asmx file contains the WebService processing directive and serves as the addressable entry point for the XML Web service.

<%@ WebService Language="VB" Codebehind="Service1.asmx.vb" Class="ws1.Service1" %>

Page 61: Visual Basic.NET ASP.NET Web Services February 25, 2004

The .asmx.vb file

• The .asmx.vb class file is a hidden, dependent file of the web service.

• It contains the code-behind class for the XML Web service.

• When viewing the Code View (F7) of the web service, you see the contents of this file.

Page 62: Visual Basic.NET ASP.NET Web Services February 25, 2004

The .asmx.vb filePublic Class Service1 Inherits System.Web.Services.WebService...

' WEB SERVICE EXAMPLE <WebMethod()> Public Function HelloWorld() As String

HelloWorld = "Hello World"End Function<WebMethod()> Public Function PersonalHello(name As String)

return "Hello "+nameEnd Function

Page 63: Visual Basic.NET ASP.NET Web Services February 25, 2004

Other files

• The Web.config file– An XML-based file that contains configuration

information for ASP.NET resources

• The .vsdisco file– An XML-based file used by ASP.NET's

dynamic XML Web service discovery process, which uses this file to identify searchable paths on the Web server.

Page 64: Visual Basic.NET ASP.NET Web Services February 25, 2004

Testing your web service

• If you run your web service in Visual Studio.NET, it brings up a browser window and navigates to the .asmx page.

• This page contains all the web methods supported by the service.

• You also get a link to browse the WSDL service description file.

• Clicking on the method itself brings up a page which shows the various request and response formats.

• If the web method has simple parameters, a test harness allows you to invoke and debug the service.

Page 65: Visual Basic.NET ASP.NET Web Services February 25, 2004
Page 66: Visual Basic.NET ASP.NET Web Services February 25, 2004

Adding the Web Service Client

Page 67: Visual Basic.NET ASP.NET Web Services February 25, 2004

Creating a Web Service Client• Using Visual Studio create both a Hello Web Service

and then a HelloClient which accesses the Hello Web Service.

• Create a few labels, a textbox, and a couple of buttons• Add the Web Service Reference• Using the following code examples:

– Add the using statement– Add the buttonclick code for each button– Remember:

• Instantiate the web service object• Use the web service object to execute the method

Page 68: Visual Basic.NET ASP.NET Web Services February 25, 2004

Adding a Web Service Proxy Reference

Manual Steps to Create

1. Generate the Proxy Class Source File

wsdl /l:vb http://localhost/...filename.asmx?wsdl

2. Compile the Proxy Class Source File

vbc /out:bin\filename.dll /t:library

/r:system.dll,system.web.dll,system.web.services.dll

filename.vb

3. Copy the dll to the Project Directory

copy bin\filename.dll

c:\inetpub\wwwroot\projectname\bin

Page 69: Visual Basic.NET ASP.NET Web Services February 25, 2004

Adding a Web Service Proxy Reference using Visual Studio.NET

• Create the ASP.NET Web Application• In the Solution Explorer, right click References and select

Add Web Reference (You can rename the web reference here if you wish).

• Select the desired Web Service or enter following url...

http://localhost/projectname.asmx• Visual Studio.NET queries the service and the WSDL file

and creates a proxy object for you. You can now instantiate and use objects of the service class as if they were in a local assembly.

Page 70: Visual Basic.NET ASP.NET Web Services February 25, 2004

Adding a Web reference

Page 71: Visual Basic.NET ASP.NET Web Services February 25, 2004

Add Client Code for Button Click EventsImports HelloClient.localhost Add an Imports statement

Private Sub Button1_Click(object sender, System.EventArgs e) Handles Button1

Public obj As New Service1() Instantiate the service object

Label3.Text = obj.HelloWorld() Execute the method.

End Sub

Private Sub Button2_Click(object sender, System.EventArgs e) Handles Button2

If TextBox1.Text = "" Then

Label3.Text = "First Name Required!"

Else

Public obj As New Service1()

Label3.Text = obj.PersonalHello(TextBox1.Text)

End If

End Sub

Page 72: Visual Basic.NET ASP.NET Web Services February 25, 2004
Page 73: Visual Basic.NET ASP.NET Web Services February 25, 2004

Final Homework / Class Project

Page 74: Visual Basic.NET ASP.NET Web Services February 25, 2004

Final Homework / Class Project

• See Project Description on web.

• Work in teams or two or three.

• Complete work by 9 PM next Wednesday (March 3, 2004)

• Be sure to capture and publish your learnings.

• Each team will present / demonstrate their solution plus discuss any interesting learnings (roughly 15 minutes each).