Presentation Transcript
Web Services with .Net :Web Services with .Net Presented by
Amrik Goswami
Overview :Overview What is a Web Service
Web Service Design Principals
Exploring the ‘Façade’ pattern
Creating a Web Service
Attributes
Returning simple and complex objects
Soap Headers and Extensions
Consuming a Web Service
IAsyncResult
Security
Deploying Web Services
What is a Web Service :What is a Web Service Hype, mainly generated by marketing people…
What is a Web Service… Really!! :What is a Web Service… Really!! The execution of code on a remote machine
The idea has been around for a long time!!!
Remote Procedure Call (RPC)
Distributed Component Object Model (DCOM)
Enterprise Java Bean (EJB)
WinSock (create your own protocol)
What is a Web Service cont… :What is a Web Service cont… XML is the primary means of communication, called ‘SOAP’
Usually over port 80 (HTTP), but can use any TCP port
Serializes all method calls into SOAP
Returns serialized objects in a SOAP format
What is a Web Service cont… :What is a Web Service cont… Pros
Portable across platforms
IBM, Microsoft, BEA, Sun and the WC3 support it
Cons
Slower than most traditional RPC methods because SOAP is VERY longwinded
Still immature and undergoing change (BE WARNED)
Web Service Design Principals :Web Service Design Principals Different to most popular RPC
Everything is ‘SingleCall’
Not Object Orientated (OO)
‘Stateless’
No properties (you can but…don’t)
Limited OO, the more OO the slower it will be
One common pattern is the ‘Façade’
Exploring the Façade pattern :Exploring the Façade pattern Think of an a Car steering wheel
Hides complexity behind a well defined interface
Ideal for Web Services
Creating a Web Service :Creating a Web Service Create a new ASP.Net Web Service project
You get an .asmx and an .asmx.cs
using System.Web.Services;
Derive your class from WebService
Add methods to your class and mark them with [WebMethod()]
Creating a Web Service :Creating a Web Service DEMO
basicws.asmx
WebService Attribuites :WebService Attribuites Description – Provides a nice description
Name – Used for ‘Aliasing’
Namespace – Effects the WSDL output
Defaults to http://tempuri.org/
You should change this when developing your WebService and DEFINITELY before deploying it
WebMethod Attribuites :WebMethod Attribuites Description – Provides a nice description
MessageName – Used for ‘Aliasing’ methods with same name but different signatures
BufferResponse – Speeds up responses
CacheDuration – Less load on resources
TransactionOption – Enterprise transactions
EnableSession – Allow you to read the IIS Session object from the Web Service
WebMethod Attributes :WebMethod Attributes DEMO
Basicws.asmx
Returning Simple Types :Returning Simple Types Simple to return ‘simple’ types
[WebMethod()]
public int DoSomething(){ return 1; }
Returns a simple Xml response
1
Sidebar… :Sidebar… Compatibility…
using System.Web.Services.Protocols;
[SoapRpcMethod()]
[WebMethod()]
public int DoSomething(){ return 1; }
Returns a different style of Xml
Simple Types and SoapRpcMethod :Simple Types and SoapRpcMethod DEMO
Basicws.asmx
Returning Simple Objects :Returning Simple Objects Same as simple types
Properties…
Need to be read/write
Constructors…
Need to have constructor with NO parameters
Methods cannot be serialized
Returning Complex Objects :Returning Complex Objects using System.Web.Services.Protocols;
To return a proxy of a custom type:
[XmlIncude(type)]
To customise the ‘Body’:
[XmlAttribute()]
[XmlElement(elementname)]
Returning Simple and Complex objects :Returning Simple and Complex objects DEMO
Complex.asmx
Soap Headers :Soap Headers Allows you to pass extra information/class to WebMethods
using System.Web.Services.Protocols;
Derive a class from SoapHeader
Create a public object of the class you want to pass within the WebService
Soap Headers :Soap Headers Add the [SoapHeader(“ClassInstance”)] attribute to the WebMethod
Create an instance of the class
Set the InstanceClassValue property of the WebService
Call the WebMethod and get a handle on the SoapHeader
Soap Headers :Soap Headers DEMO
Headers.asmx
Soap Extensions :Soap Extensions Allows you to inspect Soap and different stages
You can modify the Soap
Encryption
Compression
Logging
Derive from SoapExtension
Use ChainStream to get a writable stream
Use SoapMessageStage in the ProcessMessage method to process the message
Implement the other required methods
Soap Extensions :Soap Extensions ProcessMessage has several SoapMessageStages:
BeforeSerialize – intercept before serialized
BeforeDeserialize – intercept messages before they are deserialized *
AfterSerialize – intercept messages after they are serialized *
AfterDeserialize – intercept before processed
Soap Extension :Soap Extension DEMO
Consuming Web Services :Consuming Web Services ‘Add Web Reference’ to your project
ASP.Net, Windows Forms, Console, Windows Service, COM+
Any .NET project…
Using WSDL.exe
Wsdl /out:proxyclassfilename.cs mywsdlfile.wsdl
Add generated proxy file to project then call methods
Creating Proxy objects
Consuming Web Services :Consuming Web Services Call WebMethods just like ordinary methods
‘Update Web References’ when you add new stuff
Regenerate proxy with WSDL.exe when you add new stuff, re-add the proxy class file
Consuming and WSDL :Consuming and WSDL DEMO
IAsyncResult (Callback method) :IAsyncResult (Callback method) Create an AsyncCallback
Create a method that takes an IAsyncResult parameter
Call the Begin… method and pass in any parameters, the AsyncCallback and the proxy
IAsyncResult (WaitOne method) :IAsyncResult (WaitOne method) Call the Begin… method and pass in any parameters, null as the Callback and null as the proxy
Call WaitOne until IsCompleted is true
Async Calls :Async Calls DEMO
Security :Security Often security is an afterthought
It IS IMPORTANT !!
Credentials are NOT passed to WebServices even when you use impersonation
Web.Config file is STILL important to WebServices
You must ‘Enable’ it
Security :Security Credentials (Point-To-Point)
Set the Credentials property
You can also create credentials at run-time
Based on Windows accounts
Tickets (Application)
Needs to be passed with every method call
Custom, roll your own
Soap Header based
Security :Security WS-Security (End-To-End)
Emerging standard
Can use Custom, Binary, Kerberos Tokens
X.509 digital certificates
Soap Header based
Need WS Enhancements V1.0 (V2.0 in preview)
Soap Extensions
Can provide some basic message level encryption
Security :Security Passing Credentials
Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
You can also create your own credentials and attach them to the request
Watch out for WS-Security…. things are changing…
Security :Security DEMO
Deployment :Deployment When you ‘Add a Web Reference’ the URL property is HARD CODED
WSDL contains the URL from the site it was generated
To the rescue…
the proxy has a URL property that you can set to point to the WebService
You can implement Load Balancing…
Remember the MSI…
Create Web Setup projects to deploy your WebService
Deployment :Deployment DEMO
Summary :Summary Beware of the HYPE!!
Remember to design differently
Use Attributes to customise your Xml
As a minimum, set the Description and the Namespace
Think (consider…) Async
Be secure!!
Use the deployment tools you have in the box
References :References Patterns and Practices:
Building Secure ASP.NET Applications: Authentication, Authorization, and Secure Communication
MSDN:
Search for ‘Web Services’
MCAD/MCSD Self Paced Training:
Developing XML Web Services and Server Components with Visual Basic.NET and Visual C#.NET, Microsoft Press