76
Exposing and Consuming Web Services with .NET Tutorial 33

Exposing and Consuming Web Services with.NET Tutorial 33

Embed Size (px)

Citation preview

Page 1: Exposing and Consuming Web Services with.NET Tutorial 33

Exposing and Consuming Web Services with .NET

Tutorial 33

Page 2: Exposing and Consuming Web Services with.NET Tutorial 33

XML Web Servicesin .NET

casey# (pronounced kc-sharp) chesnut

brains-N-brawn.com

OOPSLAOctober 15, 2001

Page 3: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 3

Presentation Style

• Informal• Interrupt for topical questions

– Notice agenda• Save off topic questions until end• Fun

Page 4: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 4

Agenda

• Introduction• Standards• Exposing• Consuming• Non functional• Case Study• Questions

Page 5: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 5

Hype

• 3rd generation of the Internet– Distributed communication

• Government and Academic• No revenue model

– HTML presentation of backend DBMS• Dot Com• Advertising crash

– Web Services …• Most compelling reason to .NET

Page 6: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 6

Where They Fit In .NET

Base Class LibraryBase Class Library

Common Language SpecificationCommon Language Specification

Common Language RuntimeCommon Language Runtime

ADO.NET: Data and XMLADO.NET: Data and XML

VBVB C++C++ C#C#V

isual S

tud

io.N

ET

Visu

al Stu

dio

.NE

T

ASP.NET: Web ServicesASP.NET: Web Servicesand Web Formsand Web Forms

JScriptJScript ……

WindowsWindowsFormsForms

Page 7: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 7

What Are They?

• Application logic exposed programmatically over the internet

• Based on standards that are neither platform, vendor, language dependent

• Data and functions, NOT user interface• COTS on the web

Page 8: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 8

DEMO – Hello World

• Build from scratch (Without VS.NET)• Minimal client

– Send/Receive HTTP– Process XML

Page 9: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 9

Why Do We Need Them?

• Current distributed computing paradigms are lacking

• Make business logic accessible everywhere– Internal and external systems– Variety of clients

Page 10: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 10

Current Distributed Computing Paradigms

• DCOM, RMI, IIOP– Heavyweight– Not interoperable

• Screen-scraping– Brittle

Page 11: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 11

Business Logic Accessibility (Systems)

WebService

WebService

BusinessPartner

HTMLFront-End

IntranetApp

Page 12: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 12

Business Logic Accessibility (Clients)

WebService

GenericBrowser

WirelessCell Phone

WirelessPocket PC

DedicatedClient

?

Page 13: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 13

Who Is Involved?• Microsoft• DevelopMentor• UserLand• IBM• Lotus• Ariba• HP• Sun• W3C• BEA• …

Page 14: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 14

How Do They Work?

ConsumerWeb Service

Provider

Description

Discovery

Communication

Page 15: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 15

Web Service Standards

• Discovery (XML)– UDDI– DISCO

• Description (XML)– WSDL

• Communication (XML, HTTP …)– SOAP, XSD

• OTHER– SOAP-RP, DIME, XLANG, WSFL, XSL(T) …

Page 16: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 16

Discovery

• UDDI (Universal Discovery and Description Interface)– Directory

• DISCO– Static– Dynamic

• Word of Mouth, …

Page 17: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 17

UDDI

• Centralized directory of Business and Web Services

• Has an API which is exposed as a Web Service

• Replicated among uddi.microsoft.com, IBM, Ariba is back in! … HP soon

• uddi.org/specification.html

Page 18: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 18

DISCO

• XML file for discovering a service contract, and related resources.

• Web crawlers can be used to auto discover services

• Least solid of the specs• msdn.microsoft.com/xml/general/

disco.asp

Page 19: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 19

DISCO Examples

• Static:<disco:discovery>

<scl:contractRef ref=‘service.wsdl' docRef=‘documentation.htm'>

</disco:discovery>• Dynamic:

<?xml version="1.0" ?><dynamicDiscovery xmlns="urn:schemas-

dynamicdiscovery:disco.2000-03-17"></dynamicDiscovery>

Page 20: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 20

DEMO - DISCO

• http://localhost/default.vsdisco

Page 21: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 21

Description

• WSDL (Web Service Description Language)

• Describes protocols, operations, parameters, return values, …

• Used to generate proxy objects so clients can easily consume the service

• msdn.microsoft.com/xml/general/wsdl.asp

Page 22: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 22

XML Schema

• Define content, structure, semantics of XML documents

• Like a DTD, but written as XML• Replaces DTDs• W3C Recommendation in May• Schema repositories

Page 23: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 23

DEMO – WSDL

• Show auto-generated WSDL file

Page 24: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 24

Communication• HTTP GET

– Parameters in query string (limited)• HTTP POST

– Name/value pairs in body (ad hoc)• SOAP

– Predominantly HTTP POST w/ XML– SMTP for asynchronous calls …– Complex data types

Page 25: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 25

Marshal Types

• HTTP GET/POST– Primitives– Enums– Arrays of above

• SOAP– Primitives, Enums– Classes, Structs– DataSets– XmlDoc, XmlNodes– Arrays of above

Page 26: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 26

SOAP

• SOAP (Simple Object Access Protocol)– SOAP == HTTP + XML [de facto]– SOAP != HTTP + XML[SOAP <> HTTP + XML … for VB’ers ]

• Message Format• SMTP …

• msdn.microsoft.com/xml/general/soapspec.asp

Page 27: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 27

SOAP/XML/HTTP

• HTTP is the transport protocol– Other protocols can be used as well

• SOAP is the wire protocol (XML)– What the message should look like on

the wire• XML is used to describe the SOAP

message and the data being passed and returned to the service

Page 28: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 28

SOAP Message

• HTTP Request/Response– HTTP Headers

• SOAPAction Header

– HTTP Body• SOAP Envelope

– Optional SOAP Headers– Mandatory SOAP Body

» Boxcar Method calls

– Referenced XML Data

Page 29: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 29

Entire SOAP Message

POST /news/groups.asmx HTTP/1.1Content-Length: 504Content-Type: text/xmlHost: localhostUser-Agent: MS Web Services Client Protocol

1.0.2204.21SOAPAction:

"http://tempuri.org/GetNewMessageHeaders“// comment // followed by CRLF’s before HTTP body

Page 30: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 30

Entire SOAP Message …<?xml version="1.0"?><soap:Envelope xmlns=“”> <soap:Body> <GetNewMessageHeaders

xmlns="http://tempuri.org/"> <DateYYMMDD>010618</DateYYMMDD> <TimeHHMMSS>164431</TimeHHMMSS> <GroupName>dnug.test</GroupName> </GetNewMessageHeaders> </soap:Body></soap:Envelope>

Page 31: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 31

SOAP Request<?xml version="1.0"?><soap:Envelope xmlns:soap=“…”

xmlns:soapenc=“…” xmlns:xsi=“…" xmlns:xsd=“…">

<soap:Body> <HelloTo xmlns="http://tempuri.org/"> <Name>casey</Name> </HelloTo> </soap:Body></soap:Envelope>

Page 32: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 32

SOAP Response

<?xml version="1.0"?><soap:Envelope xmlns:soap=“…”

xmlns:soapenc=“…" xmlns:xsi=“…" xmlns:xsd=“…">

<soap:Body> <HelloToResult xmlns="http://tempuri.org/"> <result>Hello casey</result> </HelloToResult> </soap:Body></soap:Envelope>

Page 33: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 33

SOAP Fault<?xml version="1.0"?><soap:Envelope xmlns:soap=“…"> <soap:Body> <soap:Fault> <soap:faultcode>Server</soap:faultcode>

<soap:faultstring>BAD</soap:faultstring> <soap:faultactor>URL</soap:faultactor>

<soap:detail/> </soap:Fault> </soap:Body></soap:Envelope>

Page 34: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 34

SOAP Process• Client calls method on proxy object• Proxy object serializes parameters, and sends

SOAP request• Server receives SOAP request, and deserializes

parameters• Method is run on server• Results are serialized and send as a SOAP

response or fault• Proxy object receives SOAP, and deserializes

the results to return to client

Page 35: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 35

<BREAK></BREAK>

Page 36: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 36

Coding .NET Web Services

• Plumbing is automatically taken care of:– UDDI does not have to be done– DISCO is auto– WSDL is auto– SOAP de/serialization is auto

• Concentrate on business logic• Concentrate on non-func. requirements

Page 37: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 37

Demo – VS.NET

• Create simple Web Service• Consume with auto-generated client

Page 38: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 38

Passing Parameters

• Primitives for the auto-generated help page

• out parameters are SOAP only• SOAP Headers

– SoapHeader Class– SoapHeader Attribute

• in/out direction• required bool

Page 39: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 39

Return Value(s)

• You have 1 return value … use it wisely• Throw exception, not HRESULT style• SOAP can manage multiple returnspublic string RetTwoStrings(out string b)Returned as <b>value</b> following

Mandatory Soap BodyDoes not work for HTTP Get/PostReturn null for empty sets

Page 40: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 40

DEMO - DataSet

• In-memory Database– tables, rows, relations, constraints

• Can strongly-type with XSD– schema compiler

• Built-in support for errors– Tables – bool HasErrors()– Rows – Rows[] GetErrors()

Page 41: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 41

Exceptions

• Exceptions are promoted to SoapExceptions

• SoapExceptions are serialized to SOAP faults

• Also the possibility for standard HTTP exceptions before SOAP processing occurs

• Set errors within DataSets/XmlDocs

Page 42: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 42

State(less)

• Should design stateless, or pass state in method calls/headers … unless state is too large to keep passing back and forth

– Session State– Application State

• Shared among all instances of service

Page 43: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 43

Caching

• SOAP response payloads can be cached on the server

• Beta 1 Hack– Write off to XmlDocument on server,

and then return that XmlDoc based on time

Page 44: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 44

Asynchronous

• Automatically get Begin/End methods

Services s = new Service();IAsyncResult ar = s.BeginLongTime(null);//waitif(ar.IsCompleted == true){string result = s.EndLongTime(ar);}

Page 45: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 45

AsynchronousBeginMethod()

IsComplete()

false

IsComplete()

true

EndMethod ()

result

Page 46: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 46

WebService Attributes

[WebService…(Description=“blah”)](Description=“<a href>docs<a>”)](Namespace=“http://dnug.net/service”)](Name=“ServiceName”)]

Page 47: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 47

WebMethod Attributes[WebMethod…(Description=“blah”)](Transaction=Transaction.Required)](EnableSessionState=False)](CacheDuration=20)](BufferResponse=“true”)](MessageName=“MethodName”)]

Page 48: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 48

Help Page

• Modify ASP.NET web configuration• Modify default ASP.NET web help page

– Documentation– Branding– GET/POST– …

Page 49: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 49

XML Attributes

[XmlInclude(typeof(foo))]

[XmlAttribute()] [SoapAttribute()][XmlElement()] [SoapElement()][XmlArray()] [SoapArray()]

Page 50: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 50

Entry Points• Global.asax

– Application_Begin/EndRequest()– Session_Start/End()– HTTP Request/Response

• SOAP Extensions– SOAP Message before request

deserialization and after response serialization

– Roll your own SOAP implementation

Page 51: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 51

DEMO – SOAP Extension

• Request / Response• Before / After (De)serialization• Client / Server

Page 52: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 52

Service Design Tips

• Don’t change interface; additional data can be returned without harm

• Make flexible to support all possible clients

• Validate on server, you do not control the client

• Provide test instance for people to code against

Page 53: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 53

Service Design Tips

• Provide client examples for different consumers

• Provide simple type example for testing• Find a standard schema to use• Ping()• Beware denial of service• Make chunky calls, reduce network trips• …

Page 54: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 54

Service Design

WebService

Component

Component

Consumer

chunkychatty

Page 55: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 55

<BREAK></BREAK>

Page 56: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 56

DEMO – Consuming Services

• Create client• Add Web Reference• Call method• Display DataSet

Page 57: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 57

Discovering

• Can discover at design or run time– Web based registry– Exposed as web services

Page 58: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 58

UDDI tModel

ClientUDDI

Repository

MyWeb Service

Competitor’sWeb Service

tModel

URL

Page 59: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 59

Proxy

• Can occur at design or run time• SOAP by default• Store URL in XML config file• Firewall authentication• Timeout• Cookie support

Page 60: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 60

Exceptions

• HTTP• SOAP

Try{//make web service call}Catch(SoapException){ //SOAP }Catch(Exception){ //HTTP }Finally {}

Page 61: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 61

DEMO - Exceptions

• Parsing parameters• SOAP Fault• HTTP Error

Page 62: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 62

Client Design Tips

• Cache data if disconnected• Have fail-over if a service is down• Dynamically discover services based on

a schema• Don’t code to ith-index, reference Xml

nodes explicitly in case schema changes

Page 63: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 63

Non-Functional

• Security• Auditing• Deployment• Reliability• Performance• Maintainability• Usability• Scalability

Page 64: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 64

Security

• IIS (Basic, Digest, Kerberos, …)• User/Pass as Service parameters• Login method which returns a session,

make session a parameter of other calls• User/Pass in Soap Header• Certificates / SSL / HTTPS• Passport Service• Public/Private encrypt at client, server

Page 65: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 65

DEMO - Encryption

• Encryption algorithms are platform/language independent

• Symmetric• PGP

Page 66: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 66

What Else Is There?

• .NET Remoting• XML-RPC• ebXML• BXXP• BizTalk• ATL Server

Page 67: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 67

When Do We Start?

• Beta 2• SOAP Toolkit is for DNA projects• .NET supercedes SOAP Toolkit• More wireless clients all the time• More specs. coming all the time

Page 68: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 68

3rd Party Services

• HailStorm / My Services• UDDI• eBay• XMethods• SalCentral• …

Page 69: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 69

Case Study - Problem

• ListServs– Fills up email box and harddrive– Moderated vs Unmoderated lists

• Newsgroups– Lacks notification feature– Port 119 might be blocked

Page 70: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 70

Case Study – Architecture

NewsServer

NNTPWeb

Service

DedicatedClient

Firewall

OutlookExpress

Page 71: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 71

Case Study – Future

MS NewsServer

NNTPWeb

Service

DedicatedClient

@HomeNewsServerWeb

Client

MobileClient

OutlookExpress

Page 72: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 72

SOAP over NNTP

• True Asynchronous• Messaging is almost identical to SMTP• WSDL?

Page 73: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 73

Other Demos

• XSLT• User Objects• Typed DataSet• XML Server Control

Page 74: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 74

Summary

• Web Services allow heterogeneous distributed systems to communicate

• Standard adoption and support is growing

• Powerful new model for application development

• .NET Framework makes developing Web Services easy

• Likely to live-up to the hype

Page 75: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 75

Online Resources

• msdn.microsoft.com/webservices• msdn.microsoft.com/theshow• W3C.org• soap-wrc.com• xmethods.com• sys-con.com/webservices

Page 76: Exposing and Consuming Web Services with.NET Tutorial 33

Copyright © 2001 by iigo, Inc. 76

Questions

• ???