Week 5- ppt - Monash University
Download
Report
Transcript Week 5- ppt - Monash University
.NET XML Web Services
Monash University
Semester 1, March 2006
www.monash.edu.au
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
2
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
3
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
4
Service Oriented Architecture
Broker
Search
Publish
Consumer
Provider
Bind/Invoke
www.monash.edu.au
5
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 internetbased protocols.”
www.monash.edu.au
6
Distributed Programming Model
Server
Client
Web Services Description Language
(WSDL)
Client Proxy
Server Stub
SOAP, XML-RPC
Transport (HTTP, FTP, IIOP etc.)
www.monash.edu.au
7
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
8
Web Services and Firewalls
Consumer
U
RMI
Service
Port 80
Web
Service
U
DCOM
Service
Firewall
www.monash.edu.au
9
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
10
Service Oriented Architecture
UDDI
Broker
Search
Publish
WSDL
Applications
Consumer
Provider
Bind/Invoke
www.monash.edu.au
11
Business Applications n-tier
Communication
Web Service
Web Service
www.monash.edu.au
12
ASP.NET XML Web Services
• Must use an .ASMX file to create or generate
a web interface
• An ASMX file can contain code or point to a
DLL using Code behind
• Must inherit from
System.Web.Services.WebService or
implement
System.Web.Services.WebService as a
custom attribute of your class
www.monash.edu.au
13
Demonstration HelloWorldWS
• Basic Hello World in
.NET
www.monash.edu.au
14
Create a Web Service in ASP.NET
• Declaring Web Services
– .ASMX file
<%@ WebService Language="C#" Class=“MonashIT.HelloWorld,MonashITLibrary" %>
• Implementation
– .CS file
public class HelloWorld: System.Web.Services.WebService
{
[WebMethod]
public string Hello ()
{
return “Hello World”;
}
}
www.monash.edu.au
15
Web Method
• Description - a property which makes a
description available to a WSDL file
• MessageName - the method name used
when calling a Web Method
• Enable Session - defines whether a session
state is enabled
• TransactionOption - indicates transaction
support
• CacheDuration – defines the length of time
the method invocation should be cached for
www.monash.edu.au
16
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
17
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
18
Consuming Web Services
• Accessing Web Services from IE
– Demonstration
• Proxy class
• WSDL tool
– Generate source file. Need to compile
– Default is in C#
wsdl http://localhost/hello/HelloWorld.asmx?wsdl
• Visual Studio .NET automates this process by creating
www.monash.edu.au
19
Demonstration
• A stock service demonstration
http://localhost/StockTradingService/StockService.asmx
localhost/WapStockClient/Stocks.aspx
www.monash.edu.au
20
WS State Management
• Statement management objects
– Application
– Session
• EnabledSession property in WebMethod
attribute
– To enable Session object.
www.monash.edu.au
21
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
22
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
23
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
24
State Management (e.g.)
• Declaring a Web service method, with
the EnableSession property equal to
WebMethod attribute to true.
[WebMethod(EnableSession=true)]
public string YourName(string name)
{
if(Session["Name"] == null)
Session.Add("Name", name);
return Session["Name"].ToString();
}
[WebMethod(EnableSession=true)]
public string GetName()
{
if(Session["Name"] == null)
return "no name recorded";
else
return Session["Name"].ToString();
}
www.monash.edu.au
25
Caching
Key
• Uses the ASP.NET Caching Model
• Improves the performance of application by
caching messages
Normal
Cached
Complier
Output Cache
Parser
Memory
Assembly
Cache
www.monash.edu.au
26
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
27
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
28
Demonstration
• Building an application that supports
asynchronous invocation
www.monash.edu.au
29
Transaction Support in Web Services
• Same as COM+ and Microsoft Transaction
Server
• Declarative Transaction
• TransactionOption property in WebMethod
attribute
www.monash.edu.au
30
Windows security
• Client application
– NetworkCredential class
> Username, password, domain
– CredentialCache class
– Credentials property in proxy class
www.monash.edu.au
31
Web Services Security
• ASP.NET Security
– Windows security, IIS security
– Web.config file
• Customised SOAP-based security
– SOAP Header
www.monash.edu.au
32
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
33
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
34
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
35
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
36
WSE 2.0 Demo
• Using Asymmetric Encryption
• Using digital certificates
www.monash.edu.au
37
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
38
Communication between systems
www.monash.edu.au
39
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
www.monash.edu.au
40