You are on page 1of 40

Web Services with .

Presented by Paul Turner


What is a Web Service Web Service Design Principals Exploring the Faade pattern Creating a Web Service

Soap Headers and Extensions Consuming a Web Service Deploying Web Services
IAsyncResult Security

Attributes Returning simple and complex objects

What is a Web Service

Hype, mainly generated by marketing people

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

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

Portable across platforms IBM, Microsoft, BEA, Sun and the WC3 support it Slower than most traditional RPC methods because SOAP is VERY longwinded Still immature and undergoing change (BE WARNED)


Web Service Design Principals

Different to most popular RPC Everything is SingleCall Not Object Orientated (OO)
Stateless No properties (you can butdont) Limited OO, the more OO the slower it will be

One common pattern is the Faade

Exploring the Faade pattern

Think of an a Car steering wheel Hides complexity behind a well defined
interface Ideal for Web Services

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


WebService Attribuites
Description Provides a nice description Name Used for Aliasing Namespace Effects the WSDL output
Defaults to
You should change this when developing your WebService and DEFINITELY before deploying it

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

Returning Simple Types

Simple to return simple types
[WebMethod()] public int DoSomething() { return 1; } Returns a simple Xml response
<?xml version="1.0" encoding="utf-8" ?> <int xmlns="">1</int>

using System.Web.Services.Protocols; [SoapRpcMethod()] [WebMethod()] public int DoSomething() { return 1; } Returns a different style of Xml

Simple Types and SoapRpcMethod


Returning Simple Objects

Same as simple types Properties
Need to be read/write

Need to have constructor with NO parameters

Methods cannot be serialized

Returning Complex Objects

using System.Web.Services.Protocols; To return a proxy of a custom type:

To customise the Body:

[XmlAttribute()] [XmlElement(elementname)]

Returning Simple and Complex objects


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
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 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
ProcessMessage has several
BeforeSerialize intercept before serialized BeforeDeserialize intercept messages before they are deserialized * AfterSerialize intercept messages after they are serialized * AfterDeserialize intercept before processed

Soap Extension

Consuming Web Services

Add Web Reference to your project
ASP.Net, Windows Forms, Console, Windows Service, COM+ Any .NET project Wsdl /out:proxyclassfilename.cs mywsdlfile.wsdl Add generated proxy file to project then call methods

Using WSDL.exe

Creating Proxy objects

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


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)

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

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

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

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

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


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


Beware of the HYPE!! Remember to design differently Use Attributes to customise your Xml
Think (consider) Async Be secure!! Use the deployment tools you have in the
As a minimum, set the Description and the Namespace

Patterns and Practices:
Building Secure ASP.NET Applications: Authentication, Authorization, and Secure Communication 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