build message-based web services for soa

44
Build Message-Based Build Message-Based Web Services for Web Services for Service Oriented Service Oriented Architecture Architecture Jeffrey Hasan, MCSD Jeffrey Hasan, MCSD President President Bluestone Partners, Inc. Bluestone Partners, Inc.

Upload: jhasan85

Post on 20-Jan-2015

2.625 views

Category:

Technology


0 download

DESCRIPTION

My presentation covers the principles of message-based Web services and how to build them using .NET. (Note, this presentation preceded the release of WCF, and covers ASMX technology. But the core principles are still applicable).

TRANSCRIPT

Page 1: Build Message-Based Web Services for SOA

Build Message-Based Build Message-Based Web Services for Service Web Services for Service Oriented ArchitectureOriented Architecture

Jeffrey Hasan, MCSDJeffrey Hasan, MCSDPresidentPresidentBluestone Partners, Inc.Bluestone Partners, Inc.

Page 2: Build Message-Based Web Services for SOA

Talk AgendaTalk Agenda Overview of SOAOverview of SOA Build a Message-Oriented Web ServiceBuild a Message-Oriented Web Service Build a Service-Oriented Web ServiceBuild a Service-Oriented Web Service WSDL OverviewWSDL Overview Introducing the Web Services Introducing the Web Services

Enhancements 2.0 (WSE)Enhancements 2.0 (WSE)

Page 3: Build Message-Based Web Services for SOA

About MeAbout Me Jeffrey HasanJeffrey Hasan

[email protected]@bluestonepartners.com

President, Bluestone Partners Inc.President, Bluestone Partners Inc. A technology services company based in Orange County. We A technology services company based in Orange County. We

build business applications using .NET; our speciality is build business applications using .NET; our speciality is Service Oriented Architecture.Service Oriented Architecture.

(Co-) Author of: (Co-) Author of: Performance Tuning and Optimizing ASP.NET Applications,Performance Tuning and Optimizing ASP.NET Applications,

APress, 2003. ISBN: 1590590724.APress, 2003. ISBN: 1590590724. ADO.NET Programmer’s Reference,ADO.NET Programmer’s Reference, Wrox Press, 2001. Wrox Press, 2001. Professional .NET Framework,Professional .NET Framework, Wrox Press, 2001. Wrox Press, 2001. Professional VB6 Web Programming,Professional VB6 Web Programming, Wrox Press, 1999. Wrox Press, 1999. Articles for Articles for MSDN MagazineMSDN Magazine and and Advisor Media Advisor Media publications.publications.

Page 4: Build Message-Based Web Services for SOA

New SOA BookNew SOA Book

Expert Service Oriented Expert Service Oriented Architecture in C#Architecture in C#

Using the Web Services Using the Web Services Enhancements 2.0Enhancements 2.0

Published by: APress (August 2004)Published by: APress (August 2004) http://www.bluestonepartners.com/soahttp://www.bluestonepartners.com/soa

Page 5: Build Message-Based Web Services for SOA

What is a Service?What is a Service?

A service is a distributed component that provides a A service is a distributed component that provides a well-defined interface for processing and delivering well-defined interface for processing and delivering XML messagesXML messages

A service is the basic building block of a loosely-A service is the basic building block of a loosely-coupled, distributed application.coupled, distributed application.

Page 6: Build Message-Based Web Services for SOA

SOA ExampleSOA Example

Page 7: Build Message-Based Web Services for SOA

SOA in ConceptSOA in Concept

Page 8: Build Message-Based Web Services for SOA

Services In DepthServices In Depth

Services are autonomous components that process Services are autonomous components that process well-defined XML.well-defined XML.

Services provide a well-defined interface that is Services provide a well-defined interface that is described by an XML-based document called the Web described by an XML-based document called the Web Services Definition Language (WSDL) documentServices Definition Language (WSDL) document..

Services provide endpoints that consumers and other Services provide endpoints that consumers and other services can bind to, based on the service’s port services can bind to, based on the service’s port address (typically a URL)address (typically a URL)

Services are analogous to traditional object-oriented Services are analogous to traditional object-oriented (OO), type-based components(OO), type-based components

Page 9: Build Message-Based Web Services for SOA

Services Versus OO-ComponentsServices Versus OO-Components

Services are described by a WSDL contract, Services are described by a WSDL contract, not by type libraries not by type libraries Documents metadata; ports; bindingsDocuments metadata; ports; bindings Service descriptions can be easily extendedService descriptions can be easily extended

Services provide flexible bindingServices provide flexible binding WSDL document provides metadata descriptions; clients WSDL document provides metadata descriptions; clients

can dynamically bindcan dynamically bind

Services provide a service guarantee Services provide a service guarantee Policy files document service agreements and expectationsPolicy files document service agreements and expectations

Services allow for things to go wrongServices allow for things to go wrong Addressing; Reliable MessagingAddressing; Reliable Messaging XML messages preserve the integrity of requests; and provide XML messages preserve the integrity of requests; and provide

a record of communicationa record of communication

Page 10: Build Message-Based Web Services for SOA

SOA in PracticeSOA in Practice

Page 11: Build Message-Based Web Services for SOA

SOA Architecture - BasicSOA Architecture - Basic

Page 12: Build Message-Based Web Services for SOA

Service Interface - SupportService Interface - Support

Communication requirementsCommunication requirements that the service specifies in its WSDL contract (specifically, in that the service specifies in its WSDL contract (specifically, in

its binding information). This includes the format and its binding information). This includes the format and transport protocols that the service responds to (e.g., SOAP transport protocols that the service responds to (e.g., SOAP over HTTP).over HTTP).

Security requirementsSecurity requirements that the service specifies. In .NET terms, the .asmx code-that the service specifies. In .NET terms, the .asmx code-

behind can implement code that verifies incoming XML behind can implement code that verifies incoming XML messages to ensure that they contain the required security messages to ensure that they contain the required security tokens or headers.tokens or headers.

Methods (operations)Methods (operations) that the service specifies in its WSDL contract. In .NET terms, that the service specifies in its WSDL contract. In .NET terms,

the .asmx file provides methods that correspond to the the .asmx file provides methods that correspond to the service operations, but the actual business processing should service operations, but the actual business processing should be handed off to dedicated components and workflow.be handed off to dedicated components and workflow.

Page 13: Build Message-Based Web Services for SOA

SOA Architecture - AdvancedSOA Architecture - Advanced

Page 14: Build Message-Based Web Services for SOA

WS CommunicationWS Communication

Page 15: Build Message-Based Web Services for SOA

Why Use Services?Why Use Services?

A service-based approach makes sense for building A service-based approach makes sense for building solutions that cross organizational, departmental and solutions that cross organizational, departmental and corporate domain boundaries. corporate domain boundaries.

A business with multiple systems and applications on A business with multiple systems and applications on different platforms can use SOA to build a loosely-different platforms can use SOA to build a loosely-coupled integration solution that implements unified coupled integration solution that implements unified workflows. workflows.

Page 16: Build Message-Based Web Services for SOA

How to Build Message-How to Build Message-Oriented Web ServicesOriented Web Services

Page 17: Build Message-Based Web Services for SOA

XML Messages IXML Messages I

Page 18: Build Message-Based Web Services for SOA

XML Messages IIXML Messages II <Quote><Quote> <Symbol>MSFT</Symbol> <Symbol>MSFT</Symbol> <Company>Microsoft Corporation</Company> <Company>Microsoft Corporation</Company> <DateTime>11/17/2003 16:00:00</DateTime> <DateTime>11/17/2003 16:00:00</DateTime> <High>26.12</High> <High>26.12</High> <Low>24.68</Low> <Low>24.68</Low> <Open>25.49</Open> <Open>25.49</Open> <Last>25.15</Last> <Last>25.15</Last> <Change>-0.36</Change> <Change>-0.36</Change> <PercentChange>-0.0137</PercentChange> <PercentChange>-0.0137</PercentChange> <Previous_Close>25.49</Previous_Close> <Previous_Close>25.49</Previous_Close> <High_52_Week>35</High_52_Week> <High_52_Week>35</High_52_Week> <Low_52_Week>22</Low_52_Week> <Low_52_Week>22</Low_52_Week> </Quote></Quote>

Page 19: Build Message-Based Web Services for SOA

StockTrader OperationsStockTrader Operations

Page 20: Build Message-Based Web Services for SOA

StockTrader Web Service TypesStockTrader Web Service Types

Page 21: Build Message-Based Web Services for SOA

Step #1: Design SchemaStep #1: Design Schema Step 1: Design the messages and the data Step 1: Design the messages and the data

typestypes Conceptually design what the messages and data types will Conceptually design what the messages and data types will

look like. UML class diagrams are the best way to capture this look like. UML class diagrams are the best way to capture this information.information.

Page 22: Build Message-Based Web Services for SOA

Step #2: Build XSD SchemaStep #2: Build XSD Schema Step 2: Build the XSD schema file for the data Step 2: Build the XSD schema file for the data

typestypes Use an XML designer tool to build the XSD schema file for all Use an XML designer tool to build the XSD schema file for all

of the data types that are exchanged by the Web service of the data types that are exchanged by the Web service methods. Visual Studio .NET’s XML Designer is a good tool, methods. Visual Studio .NET’s XML Designer is a good tool, but you can use any XML Designer tool that you are but you can use any XML Designer tool that you are comfortable working with.comfortable working with.

Page 23: Build Message-Based Web Services for SOA

Step #3: Create Class InterfaceStep #3: Create Class Interface Step 3: Create a class file of interface Step 3: Create a class file of interface

definitions for the messages and data types.definitions for the messages and data types. The interface definition class file provides the abstract The interface definition class file provides the abstract

definitions of the Web service methods and its data types. definitions of the Web service methods and its data types. This class file derives from the This class file derives from the System.Web.Services.WebService class, so it can be readily System.Web.Services.WebService class, so it can be readily implemented in a Web services code-behind file. The .NET implemented in a Web services code-behind file. The .NET Framework provides a command-line tool called xsd.exe for Framework provides a command-line tool called xsd.exe for generating an interface definition class file based on an XSD generating an interface definition class file based on an XSD schema file. This will manually generate class definitions for schema file. This will manually generate class definitions for the data types. You can add this class file to your Web service the data types. You can add this class file to your Web service project and then manually insert abstract definitions for the project and then manually insert abstract definitions for the Web methods.Web methods.

Page 24: Build Message-Based Web Services for SOA

Step #4: Implement InterfaceStep #4: Implement Interface Step 4: Implement the interface in the Web Step 4: Implement the interface in the Web

service code-behind fileservice code-behind file Implement code for the Web methods. The Web service .asmx Implement code for the Web methods. The Web service .asmx

code-behind class derives from the code-behind class derives from the System.Web.Services.WebService class by default, as does System.Web.Services.WebService class by default, as does the interface definition class file from Step 3. So you can the interface definition class file from Step 3. So you can derive the .asmx code-behind class directly from the interface derive the .asmx code-behind class directly from the interface definition class instead, and then implement code for each of definition class instead, and then implement code for each of the methods.the methods.

Page 25: Build Message-Based Web Services for SOA

Step #5: Generate Proxy ClassStep #5: Generate Proxy Class Step #5: Generate a proxy class file for clients Step #5: Generate a proxy class file for clients

based on the WSDL documentbased on the WSDL document Web services have no reason to exist unless they are being Web services have no reason to exist unless they are being

used by clients. In this step you generate a proxy class file used by clients. In this step you generate a proxy class file based on the Web service WSDL document so that clients based on the Web service WSDL document so that clients know how to call your Web service, and what messages and know how to call your Web service, and what messages and data types will be exchanged. The wsdl.exe command-line data types will be exchanged. The wsdl.exe command-line tool will automatically generate this proxy class for you based tool will automatically generate this proxy class for you based on the WSDL document. And Visual Studio .NET will on the WSDL document. And Visual Studio .NET will automatically generate the WSDL document for you, so no automatically generate the WSDL document for you, so no manual work is required (use Add Reference)manual work is required (use Add Reference)

Page 26: Build Message-Based Web Services for SOA

Step #6: Create ClientStep #6: Create Client Step 6: Implement a Web service client using a Step 6: Implement a Web service client using a

proxy class fileproxy class file The client essentially does nothing more than delegate The client essentially does nothing more than delegate

method calls out to the Web service. Valid clients include Web method calls out to the Web service. Valid clients include Web applications, Windows Forms applications, Console applications, Windows Forms applications, Console

applications, or even other Web services.applications, or even other Web services.

Page 27: Build Message-Based Web Services for SOA

How to Build Service-How to Build Service-Oriented Web ServicesOriented Web Services

Page 28: Build Message-Based Web Services for SOA

SOA Web ServicesSOA Web Services Web services should not implement business logic Web services should not implement business logic

directly in their methodsdirectly in their methods They should delegate this processing to dedicated business They should delegate this processing to dedicated business

assemblies. This is because you cannot assume that the assemblies. This is because you cannot assume that the business logic will always be accessed through a Web business logic will always be accessed through a Web service. service.

Web services and their associated WSDL documents Web services and their associated WSDL documents should not be the original reference points for should not be the original reference points for

interface definitionsinterface definitions This information belongs in a dedicated reference assembly, This information belongs in a dedicated reference assembly,

and should be stored as an interface definition that can be and should be stored as an interface definition that can be implemented in different kinds of components. implemented in different kinds of components.

Page 29: Build Message-Based Web Services for SOA

Revised ArchitectureRevised Architecture

Page 30: Build Message-Based Web Services for SOA

Step #1: Create a dedicated type Step #1: Create a dedicated type definition assemblydefinition assembly Step 1: Create a dedicated type definition Step 1: Create a dedicated type definition

assemblyassembly Create a dedicated definition assembly for interfaces and type Create a dedicated definition assembly for interfaces and type

definitions. This assembly will be referenced by any component, definitions. This assembly will be referenced by any component, service or application that needs to use the interfaces or types.service or application that needs to use the interfaces or types.

Page 31: Build Message-Based Web Services for SOA

Step #2: Create a dedicated Step #2: Create a dedicated business assemblybusiness assembly Step 2: Create a dedicated business assemblyStep 2: Create a dedicated business assembly

Create a dedicated business assembly that implements logic for Create a dedicated business assembly that implements logic for established interfaces and type definitions. This business assembly established interfaces and type definitions. This business assembly must reference the definition assembly from Step 1. This ensures must reference the definition assembly from Step 1. This ensures that the business assembly implements every available method that the business assembly implements every available method definition. definition.

Once this step is complete you now have the flexibility to build any Once this step is complete you now have the flexibility to build any kind of n-Tier solution using the definition and business assemblies.kind of n-Tier solution using the definition and business assemblies.

This point underscores the fact that in a Service Oriented This point underscores the fact that in a Service Oriented Architecture, Web services are simply a gateway to a set of Architecture, Web services are simply a gateway to a set of methods and types that are controlled by other assemblies. methods and types that are controlled by other assemblies. The Web service itself simply provides a set of SOAP-enabled The Web service itself simply provides a set of SOAP-enabled endpoints that are accessible over one or more transport endpoints that are accessible over one or more transport protocols.protocols.

Page 32: Build Message-Based Web Services for SOA

Step #3: Create the Web serviceStep #3: Create the Web service Step 3: Create the Web service based on the Step 3: Create the Web service based on the

type definition assemblytype definition assembly In the previous version of the StockTrader Web service the In the previous version of the StockTrader Web service the

definition information for the Web method implementations definition information for the Web method implementations came from a dedicated interface definition file, which provided came from a dedicated interface definition file, which provided abstract class definitions and class-based type definitions. abstract class definitions and class-based type definitions. But now this file is no longer needed because we have a But now this file is no longer needed because we have a dedicated definition assembly. The new Web service simply dedicated definition assembly. The new Web service simply needs to import the definition assembly to have access to the needs to import the definition assembly to have access to the required types, and to the required interface. required types, and to the required interface.

Page 33: Build Message-Based Web Services for SOA

Step #4: Implement the Interface Step #4: Implement the Interface + Import the Business Assembly+ Import the Business Assembly

Step 4: Implement the business interface in the Step 4: Implement the business interface in the Web serviceWeb service The Web service needs to import the business assembly so that it The Web service needs to import the business assembly so that it

can delegate incoming service requests. Remember, the current can delegate incoming service requests. Remember, the current architecture calls for a different level of abstraction, where the Web architecture calls for a different level of abstraction, where the Web service itself does not control its interface, its data types or the service itself does not control its interface, its data types or the processing of business logic. Instead, it relies on other assemblies processing of business logic. Instead, it relies on other assemblies for this reference information, and for this processing capability.for this reference information, and for this processing capability.

By implementing the interface you are ensured not to miss any By implementing the interface you are ensured not to miss any methods, because the project will not compile unless every interface methods, because the project will not compile unless every interface method is implemented in the Web service. So, the definition method is implemented in the Web service. So, the definition assembly provides the interface definition, while the business assembly provides the interface definition, while the business assembly provides the processing capability for each method. All assembly provides the processing capability for each method. All incoming Web service requests should be delegated to the business incoming Web service requests should be delegated to the business component, rather than implementing the business logic directly in component, rather than implementing the business logic directly in the Web service.the Web service.

Page 34: Build Message-Based Web Services for SOA

Step #5: Generate Proxy ClassStep #5: Generate Proxy Class Step #5: Generate a proxy class file for clients Step #5: Generate a proxy class file for clients

based on the WSDL documentbased on the WSDL document Web services have no reason to exist unless they are being Web services have no reason to exist unless they are being

used by clients. In this step you generate a proxy class file used by clients. In this step you generate a proxy class file based on the Web service WSDL document so that clients based on the Web service WSDL document so that clients know how to call your Web service, and what messages and know how to call your Web service, and what messages and data types will be exchanged. The wsdl.exe command-line data types will be exchanged. The wsdl.exe command-line tool will automatically generate this proxy class for you based tool will automatically generate this proxy class for you based on the WSDL document. And Visual Studio .NET will on the WSDL document. And Visual Studio .NET will automatically generate the WSDL document for you, so no automatically generate the WSDL document for you, so no manual work is required (use Add Reference)manual work is required (use Add Reference)

Page 35: Build Message-Based Web Services for SOA

Step #6: Create a Tightly-Coupled Step #6: Create a Tightly-Coupled ClientClient

Step 6: Implement a tightly-coupled Web Step 6: Implement a tightly-coupled Web service client using a proxy class file and an service client using a proxy class file and an interface definition assemblyinterface definition assembly The Web service client uses the generated proxy class file The Web service client uses the generated proxy class file

from Step 5 to set a reference to the new Web service. The from Step 5 to set a reference to the new Web service. The client must also reference the type definition assembly from client must also reference the type definition assembly from Step 1, so that they have a common understanding of the data Step 1, so that they have a common understanding of the data types that are used by the Web services and its associated types that are used by the Web services and its associated business assembly.business assembly.

This approach is warranted when you have a sensitive This approach is warranted when you have a sensitive business workflow, and you want to prevent any kind of business workflow, and you want to prevent any kind of

miscommunication between a service and a client.miscommunication between a service and a client.

Page 36: Build Message-Based Web Services for SOA

WSDL OverviewWSDL Overview

Page 37: Build Message-Based Web Services for SOA

WSDL OverviewWSDL Overview

Web Services Description LanguageWeb Services Description Language WS-I Basic Profile (WSDL 1.1)WS-I Basic Profile (WSDL 1.1)

WSDL documents:WSDL documents: Operations that the WS supportsOperations that the WS supports Messages that the WS exchanges Messages that the WS exchanges Data types that these messages useData types that these messages use

both intrinsic and custom both intrinsic and custom Everything is qualifiedEverything is qualified Can be generated from VS .NETCan be generated from VS .NET

Page 38: Build Message-Based Web Services for SOA

WSDL ElementsWSDL Elements

A WSDL Document contains seven primary A WSDL Document contains seven primary XML elements (+ the root <xml> element)XML elements (+ the root <xml> element)

Abstract descriptionAbstract description XML elements that document the Web service interface, XML elements that document the Web service interface,

including the methods that it supports, the input including the methods that it supports, the input parameters, and the return typesparameters, and the return types

Concrete implementationConcrete implementation XML elements that show the client how to physically bind XML elements that show the client how to physically bind

to the Web service and to use its supported operationsto the Web service and to use its supported operations

Page 39: Build Message-Based Web Services for SOA

WSDL Abstract ElementsWSDL Abstract Elements

<types /><types /> Equivalent to an XSD schema fileEquivalent to an XSD schema file

<message /><message /> describes a SOAP message, which may be an input, output or describes a SOAP message, which may be an input, output or

fault message for a Web service operationfault message for a Web service operation <operation /><operation />

Analogous to method definitions; groups associated Analogous to method definitions; groups associated messages into operationsmessages into operations

Indicates direction (one-way; notification; request/response)Indicates direction (one-way; notification; request/response)

<portType /><portType /> Abstract definition of a Web service; provided as a summary Abstract definition of a Web service; provided as a summary

of operationsof operations

Page 40: Build Message-Based Web Services for SOA

WSDL Concrete ElementsWSDL Concrete Elements

<binding /><binding /> Links an abstract Web service definition to an actual Web Links an abstract Web service definition to an actual Web

service, by associating the operation/message definitions to service, by associating the operation/message definitions to an XML namespacean XML namespace

<port /><port /> Documents the physical location (URI) of the Web serviceDocuments the physical location (URI) of the Web service

Contains a <binding /> elementContains a <binding /> element <service /><service />

Encloses one or more <port /> elementsEncloses one or more <port /> elements Provides a unified service definition of the abstract and Provides a unified service definition of the abstract and

concrete service elementsconcrete service elements

Page 41: Build Message-Based Web Services for SOA

WSDL Document StructureWSDL Document Structure

Page 42: Build Message-Based Web Services for SOA

WSE OverviewWSE Overview

Page 43: Build Message-Based Web Services for SOA

WSE SpecificationsWSE Specifications

Page 44: Build Message-Based Web Services for SOA

WSE OverviewWSE Overview WS-SecurityWS-Security: A wide-ranging specification that integrates a set of : A wide-ranging specification that integrates a set of

popular security technologies, including digital signing and popular security technologies, including digital signing and encryption based on security tokens, including X.509 certificates.encryption based on security tokens, including X.509 certificates.

WS-PolicyWS-Policy: Allows Web services to document their requirements, : Allows Web services to document their requirements, preferences and capabilities for a range of factors, though mostly preferences and capabilities for a range of factors, though mostly focused on security. For example, a Web service policy will focused on security. For example, a Web service policy will include its security requirements, such as encryption and digital include its security requirements, such as encryption and digital signing based on an X.509 certificate.signing based on an X.509 certificate.

WS-AddressingWS-Addressing: Identifies service endpoints in a message and : Identifies service endpoints in a message and allows for these endpoints to remain updated as the message is allows for these endpoints to remain updated as the message is passed along through two or more services. It largely replaces passed along through two or more services. It largely replaces the earlier WS-Routing specification.the earlier WS-Routing specification.

WS-MessagingWS-Messaging: Provides support for alternate transport channel : Provides support for alternate transport channel protocols besides HTTP, including TCP. It simplifies the protocols besides HTTP, including TCP. It simplifies the development of messaging applications, including asynchronous development of messaging applications, including asynchronous applications that communicate using SOAP over HTTP.applications that communicate using SOAP over HTTP.

WS-Secure ConversationWS-Secure Conversation: Establishes session-oriented trusted : Establishes session-oriented trusted communication sessions using security tokens.communication sessions using security tokens.

WS-Reliable MessagingWS-Reliable Messaging: Provides mechanisms to help ensure the : Provides mechanisms to help ensure the reliable delivery of messages even when one or more services in reliable delivery of messages even when one or more services in the chain are unavailable. This specification includes message the chain are unavailable. This specification includes message delivery notifications so that a sender knows whether a receiver delivery notifications so that a sender knows whether a receiver has successfully obtained a sent message.has successfully obtained a sent message.