390 likes | 638 Views
Overview. What is a Web ServiceWeb Service Design PrincipalsExploring the
E N D
1. Web Services with .Net Presented by
Paul Turner
pturner@eds.com
2. 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
3. What is a Web Service Hype, mainly generated by marketing people…
4. 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)
5. 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
6. 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)
7. 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’
8. Exploring the Façade pattern Think of an a Car steering wheel
Hides complexity behind a well defined interface
Ideal for Web Services
9. 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()]
10. Creating a Web Service DEMO
basicws.asmx
11. 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
12. 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
13. WebMethod Attributes DEMO
Basicws.asmx
14. 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="http://tempuri.org/">1</int>
15. Sidebar… Compatibility…
using System.Web.Services.Protocols;
[SoapRpcMethod()]
[WebMethod()]
public int DoSomething(){ return 1; }
Returns a different style of Xml
16. Simple Types and SoapRpcMethod DEMO
Basicws.asmx
17. 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
18. 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)]
19. Returning Simple and Complex objects DEMO
Complex.asmx
20. 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
21. 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
22. Soap Headers DEMO
Headers.asmx
23. 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
24. 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
25. Soap Extension DEMO
26. 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
27. 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
28. Consuming and WSDL DEMO
29. 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
30. 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
31. Async Calls DEMO
32. 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
33. 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
34. 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
35. 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…
36. Security DEMO
37. 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
38. Deployment DEMO
39. 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
40. 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