exposing and consuming web services with.net tutorial 33
TRANSCRIPT
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
Copyright © 2001 by iigo, Inc. 3
Presentation Style
• Informal• Interrupt for topical questions
– Notice agenda• Save off topic questions until end• Fun
Copyright © 2001 by iigo, Inc. 4
Agenda
• Introduction• Standards• Exposing• Consuming• Non functional• Case Study• Questions
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
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
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
Copyright © 2001 by iigo, Inc. 8
DEMO – Hello World
• Build from scratch (Without VS.NET)• Minimal client
– Send/Receive HTTP– Process XML
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
Copyright © 2001 by iigo, Inc. 10
Current Distributed Computing Paradigms
• DCOM, RMI, IIOP– Heavyweight– Not interoperable
• Screen-scraping– Brittle
Copyright © 2001 by iigo, Inc. 11
Business Logic Accessibility (Systems)
WebService
WebService
BusinessPartner
HTMLFront-End
IntranetApp
Copyright © 2001 by iigo, Inc. 12
Business Logic Accessibility (Clients)
WebService
GenericBrowser
WirelessCell Phone
WirelessPocket PC
DedicatedClient
?
Copyright © 2001 by iigo, Inc. 13
Who Is Involved?• Microsoft• DevelopMentor• UserLand• IBM• Lotus• Ariba• HP• Sun• W3C• BEA• …
Copyright © 2001 by iigo, Inc. 14
How Do They Work?
ConsumerWeb Service
Provider
Description
Discovery
Communication
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) …
Copyright © 2001 by iigo, Inc. 16
Discovery
• UDDI (Universal Discovery and Description Interface)– Directory
• DISCO– Static– Dynamic
• Word of Mouth, …
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
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
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>
Copyright © 2001 by iigo, Inc. 20
DEMO - DISCO
• http://localhost/default.vsdisco
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
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
Copyright © 2001 by iigo, Inc. 23
DEMO – WSDL
• Show auto-generated WSDL file
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
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
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
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
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
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
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>
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>
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>
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>
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
Copyright © 2001 by iigo, Inc. 35
<BREAK></BREAK>
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
Copyright © 2001 by iigo, Inc. 37
Demo – VS.NET
• Create simple Web Service• Consume with auto-generated client
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
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
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()
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
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
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
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);}
Copyright © 2001 by iigo, Inc. 45
AsynchronousBeginMethod()
IsComplete()
false
IsComplete()
true
EndMethod ()
result
Copyright © 2001 by iigo, Inc. 46
WebService Attributes
[WebService…(Description=“blah”)](Description=“<a href>docs<a>”)](Namespace=“http://dnug.net/service”)](Name=“ServiceName”)]
Copyright © 2001 by iigo, Inc. 47
WebMethod Attributes[WebMethod…(Description=“blah”)](Transaction=Transaction.Required)](EnableSessionState=False)](CacheDuration=20)](BufferResponse=“true”)](MessageName=“MethodName”)]
Copyright © 2001 by iigo, Inc. 48
Help Page
• Modify ASP.NET web configuration• Modify default ASP.NET web help page
– Documentation– Branding– GET/POST– …
Copyright © 2001 by iigo, Inc. 49
XML Attributes
[XmlInclude(typeof(foo))]
[XmlAttribute()] [SoapAttribute()][XmlElement()] [SoapElement()][XmlArray()] [SoapArray()]
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
Copyright © 2001 by iigo, Inc. 51
DEMO – SOAP Extension
• Request / Response• Before / After (De)serialization• Client / Server
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
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• …
Copyright © 2001 by iigo, Inc. 54
Service Design
WebService
Component
Component
Consumer
chunkychatty
Copyright © 2001 by iigo, Inc. 55
<BREAK></BREAK>
Copyright © 2001 by iigo, Inc. 56
DEMO – Consuming Services
• Create client• Add Web Reference• Call method• Display DataSet
Copyright © 2001 by iigo, Inc. 57
Discovering
• Can discover at design or run time– Web based registry– Exposed as web services
Copyright © 2001 by iigo, Inc. 58
UDDI tModel
ClientUDDI
Repository
MyWeb Service
Competitor’sWeb Service
tModel
URL
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
Copyright © 2001 by iigo, Inc. 60
Exceptions
• HTTP• SOAP
Try{//make web service call}Catch(SoapException){ //SOAP }Catch(Exception){ //HTTP }Finally {}
Copyright © 2001 by iigo, Inc. 61
DEMO - Exceptions
• Parsing parameters• SOAP Fault• HTTP Error
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
Copyright © 2001 by iigo, Inc. 63
Non-Functional
• Security• Auditing• Deployment• Reliability• Performance• Maintainability• Usability• Scalability
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
Copyright © 2001 by iigo, Inc. 65
DEMO - Encryption
• Encryption algorithms are platform/language independent
• Symmetric• PGP
Copyright © 2001 by iigo, Inc. 66
What Else Is There?
• .NET Remoting• XML-RPC• ebXML• BXXP• BizTalk• ATL Server
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
Copyright © 2001 by iigo, Inc. 68
3rd Party Services
• HailStorm / My Services• UDDI• eBay• XMethods• SalCentral• …
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
Copyright © 2001 by iigo, Inc. 70
Case Study – Architecture
NewsServer
NNTPWeb
Service
DedicatedClient
Firewall
OutlookExpress
Copyright © 2001 by iigo, Inc. 71
Case Study – Future
MS NewsServer
NNTPWeb
Service
DedicatedClient
@HomeNewsServerWeb
Client
MobileClient
OutlookExpress
Copyright © 2001 by iigo, Inc. 72
SOAP over NNTP
• True Asynchronous• Messaging is almost identical to SMTP• WSDL?
Copyright © 2001 by iigo, Inc. 73
Other Demos
• XSLT• User Objects• Typed DataSet• XML Server Control
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
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
Copyright © 2001 by iigo, Inc. 76
Questions
• ???