Www.monash.edu.au.NET XML Web Services Monash University Semester 1, March 2006

Preview:

Citation preview

www.monash.edu.au

.NET XML Web Services

Monash University

Semester 1, March 2006

www.monash.edu.au

2

Overview

• Current Business Models• What are XML Web Services and why use

them• How web services are developed

using .NET• Extensions to Web Services using .NET

www.monash.edu.au

3

Business Applications 3-tier

• Business applications must separate software into 3 tiers

• Separate the business process from the user interface and data layers

User Interface

Business Objects

Data Layers

www.monash.edu.au

4

Extend your business

• With 3-tier architecture, you restrict your business to your applications

• With global economy, you want different global companies to use your software

• Need to provide another tier to your business process

• Service Oriented Architecture is what can be used to expose your business to the software world

www.monash.edu.au

5

Service Oriented Architecture

ProviderConsumer

Broker

PublishSearch

Bind/Invoke

www.monash.edu.au

6

What are XML Web Services

• W3C Definition– “… software application identified by a URI,

whose interfaces and binding are capable of being defined, described and discovered by XML artefacts and supports direct interactions with other software applications using XML based messages via internet-based protocols.”

www.monash.edu.au

7

Distributed Programming Model

Client Server

Web Services Description Language (WSDL)

Server StubClient Proxy

SOAP, XML-RPC

Transport (HTTP, FTP, IIOP etc.)

www.monash.edu.au

8

Why XML Web Services

• Supports n-tier Application Architecture (Façade Pattern should be used)

• Provide a web interface to you current business infrastructure

• Use web protocols and standards to communicate and transfer information

• Can be used to communicate through firewalls that block other Distributed Programming models

www.monash.edu.au

9

WebService

RMIService

DCOMService

Consumer

Firewall

Port 80

Web Services and Firewalls

www.monash.edu.au

10

Web Services Standards and Protocols

• Open Web Protocols– HTTP, XML, SOAP etc.

• Web Services Contracts/Description– WSDL

• Advertising and Discovery– UDDI, Disco

www.monash.edu.au

11

Service Oriented Architecture

ProviderConsumer

Broker

PublishSearch

Bind/Invoke

Applications

UDDI

WSDL

www.monash.edu.au

12

Business Applications n-tier

Web Service Web Service

Communication

www.monash.edu.au

13

ASP.NET XML Web Services

• Must use an .ASMX file to create or generate a Must use an .ASMX file to create or generate a web interfaceweb interface

• An ASMX file can contain code or point to a An ASMX file can contain code or point to a DLL using Code behind DLL using Code behind

• Must inherit from Must inherit from System.Web.Services.WebService or System.Web.Services.WebService or implement System.Web.Services.WebService implement System.Web.Services.WebService as a custom attribute of your classas a custom attribute of your class

www.monash.edu.au

14

Demonstration HelloWorldWS

• Basic Hello World in .NET

www.monash.edu.au

15

Create a Web Service in ASP.NET

• Declaring Web Services– .ASMX file

• Implementation– .CS file

public class HelloWorld: System.Web.Services.WebServiceSystem.Web.Services.WebService{{

[WebMethod][WebMethod]public string Hello ()public string Hello (){{

return “Hello World”;return “Hello World”;}}

}}

<%@ WebService Language="C#" Class=“MonashIT.HelloWorld,MonashITLibrary" %>

www.monash.edu.au

16

Web Method

• Description - a property which makes a Description - a property which makes a description available to a WSDL filedescription available to a WSDL file

• MessageName - the method name used when MessageName - the method name used when calling a Web Method calling a Web Method

• Enable Session - defines whether a session Enable Session - defines whether a session state is enabledstate is enabled

• TransactionOption - indicates transaction TransactionOption - indicates transaction supportsupport

• CacheDuration – defines the length of time the CacheDuration – defines the length of time the method invocation should be cached formethod invocation should be cached for

www.monash.edu.au

17

Stateless and Statefull services

• Stateless means a web service losses its state

• Web services are normally stateless• Usually provide access to state information

such as databases and XML files• .NET Web services are stateless by default• More will be covered in Module 8

www.monash.edu.au

18

Publishing Web Services

• Files– Web.Config and Global.asax file

– Assemblies and .asmx files

• IIS structure– Must create a virtual directory

– Must ensure that the web service has appropriate permissions to access and modify resources

www.monash.edu.au

19

Consuming Web Services

• Accessing Web Services from IE– Demonstration

• Proxy class• WSDL tool

– Generate source file. Need to compile– Default is in C#

• Visual Studio .NET automates this process by creating

wsdl http://localhost/hello/HelloWorld.asmx?wsdl

www.monash.edu.au

20

Demonstration

• A stock service demonstration

http://localhost/StockTradingService/StockService.asmx

localhost/WapStockClient/Stocks.aspx

www.monash.edu.au

21

WS State Management

• Statement management objects– Application

– Session

• EnabledSession property in WebMethod attribute– To enable Session object.

www.monash.edu.au

22

State Management

• Web Services are stateless• Use ASP.NET session state mechanism

– Restricted to a logical application

– Context in which a user communicates to a server

– Functionality> Request identification and classification> Store data across multiple requests> Session events> Release of session data

– .NET State Server Process

www.monash.edu.au

23

State Management

• Each request of a Web Service its class instance is instantiated and thrown away when it is no longer used– i.e. each time you call a method the class

variable values are not available be the web service has be re-instantiated

• If data is required to be used between different requests, the ASP.NET built-in session state mechanisms must be used

www.monash.edu.au

24

State Management (cont…)

• Web services have access to the same state management options as other ASP.NET applications such as the Session and Application objects and cookies

• Data stored in the Session object is available only when the EnableSession property of the WebMethod attribute is set to true. Access to the Application object is automatic however

www.monash.edu.au

25

State Management (e.g.)

• Declaring a Web service method, with the EnableSession property equal to WebMethod attribute to true.

[WebMethod(EnableSession=true)][WebMethod(EnableSession=true)]public string YourName(string name)public string YourName(string name){{ if(Session["Name"] == null)if(Session["Name"] == null) Session.Add("Name", name);Session.Add("Name", name);

return Session["Name"].ToString();return Session["Name"].ToString();}}

[WebMethod(EnableSession=true)][WebMethod(EnableSession=true)]public string YourName(string name)public string YourName(string name){{ if(Session["Name"] == null)if(Session["Name"] == null) Session.Add("Name", name);Session.Add("Name", name);

return Session["Name"].ToString();return Session["Name"].ToString();}}

[WebMethod(EnableSession=true)][WebMethod(EnableSession=true)]public string GetName()public string GetName(){{ if(Session["Name"] == null)if(Session["Name"] == null) return "no name recorded";return "no name recorded"; elseelse return Session["Name"].ToString();return Session["Name"].ToString();}}

[WebMethod(EnableSession=true)][WebMethod(EnableSession=true)]public string GetName()public string GetName(){{ if(Session["Name"] == null)if(Session["Name"] == null) return "no name recorded";return "no name recorded"; elseelse return Session["Name"].ToString();return Session["Name"].ToString();}}

www.monash.edu.au

26

Caching

• Uses the ASP.NET Caching Model• Improves the performance of application by

caching messages

Parser

Assembly CacheMemory

ComplierOutput C

ache

Key

Normal

Cached

www.monash.edu.au

27

Web Service Execution Model

• Synchronous– Like any other call to class methods

• Asynchronous– Split the method into two code blocks

> BeginMethodName> EndMethodName

– CLR determines if and when operation has finished

www.monash.edu.au

28

Asynchronous invocation

• Exposed in generated proxy class– Begin and End method

• Begin method– Return IAsyncResult

– Parameters > AsyncCallback> Object

• End method– Return function return type

– First parameter is IAsyncResult

www.monash.edu.au

29

Demonstration

• Building an application that supports asynchronous invocation

www.monash.edu.au

30

Transaction Support in Web Services

• Same as COM+ and Microsoft Transaction Server

• Declarative Transaction• TransactionOption property in WebMethod

attribute

www.monash.edu.au

31

Windows security

• Client application– NetworkCredential class

> Username, password, domain

– CredentialCache class

– Credentials property in proxy class

www.monash.edu.au

32

Web Services Security

• ASP.NET Security– Windows security, IIS security

– Web.config file

• Customised SOAP-based security– SOAP Header

www.monash.edu.au

33

SOAP Header

• Inherit from SOAPHeader class• Declare a public attribute• Apply SOAPHeader attribute to

WebMethod– MemberName property

• However, this is .NET specific

www.monash.edu.au

34

Global XML Architecture

• GXA provides uniform way of using XML web services

• A Microsoft specific initiative to web services• Supports new extensions to web services• Implemented in .NET using Web Services

Enhancements

www.monash.edu.au

35

Web Services Enhancements 2.0

• Provides additional Web Services capabilities• Additional WS specifications

– WS-Security

– WS-Policy

– WS-Trust

– WS-Addressing

– Etc.

www.monash.edu.au

36

WS-Security

• By OASIS• Enhancements to

SOAP messaging• A general purpose

mechanism for secure messaging

• Support symmetric and public key technologies

www.monash.edu.au

37

WSE 2.0 Demo

• Using Asymmetric Encryption• Using digital certificates

www.monash.edu.au

38

Web Service Technologies

• EJB using Tomcat & Sun ONE toolkit• BEA Weblogic• IBM Web Sphere • .NET using ASP.NET and IIS• Many more available

www.monash.edu.au

39

Communication between systems

www.monash.edu.au

40

Conclusion

• Businesses use web services for interoperability

• .NET offers web services through ASP.NET

• ASP.NET provides caching and supports the Global XML architecture through Web Services extensions

Recommended