Title: Web Services
1Web Services
2What are they?
- Web services are a platform independent way to
exchange data using XML. They are components of
business logic that can be accessed over the
Internet. They are a way of re-using someone
elses logic without re-creating it yourself.
3Examples
- An e-commerce company can use a shippers web
service to calculate the exact cost of a
shipment. - National weather forecasters use them to supply
data to web-sites and news organizations. - Stock prices are provided this way by major
exchanges and corporations.
4Benefits of Web Services
- Simple easily supported on many platforms
- Loosely Coupled the interface can be extended
and new methods added without affecting clients
as long as old methods and parameters are still
provided - Stateless a request is made, then result is
returned and the connection closes, no permanent
connection - Firewall Friendly uses HTTP, not usually blocked
5When to use Web Services
- Cross platform i.e. Communicate between a Java
app and a .NET app - Cross trust boundaries between two unrelated
organizations - Future considerations if there is a possibility
that the logic may have to support third party
integration
6When to Avoid Web Services
- .Net to .Net there are better ways to
communicate between servers, such as .NET
remoting - .Net Apps rather than provide a Service, if two
apps need the same logic create a class library
assembly compiled to a DLL, which is then used in
both apps.
7Discovering and Using Services
- Right click your project in Solution Explorer
- Select Add Web Reference
- In the page that appears input the URL of a web
service (.asmx or .wsdl file) - When the service is found a listing will appear
of all the available methods and a link to
Service Description this is where you find out
how the service works - Select Add Reference and VS2008 will add all the
necessary files to your solution
8Proxy Class
- A Web Service communicates with a subscriber
using XML and SOAP (Simple Object Access
Protocol). .NET manages these protocols for you
by generating the SOAP message and transmitting
it using HTTP. The response is then converted
back to the corresponding .NET type. - SOAP Tutorial
9(No Transcript)
10Coding Limitations
- You can create your web service using any valid
.NET code. However there is no interface design
because the service is stateless. Parameter and
return types are limited to the XML Schema
standard such as strings and numbers. You can not
pass proprietary .NET objects such as FileStream
or an Event Log.
11Web Service Data Types
- Simple types int, unsigned int, float, double,
decimal, bool, string, char, byte, DateTime - Arrays of any supported type, ArrayList, byte
arrays (which will automatically be Base64
encoded) - DataSet and DataTable, but not DataColumns or
DataRows
12Creating a Web Service
- Choose Add New Item form the context menu of a
folder - Select Web Service and name the file
- The asmx file is created in the folder, this file
is not modified by you as there can be no user
interface - Another file, asmx.cs is created in your App_Code
folder. This is where your code is written
13WebService(Namespace "Seneca.Ian.Webservices",
Description "Example web service that returns
warp server information") WebServiceBinding(Conf
ormsTo WsiProfiles.BasicProfile1_1) // To
allow this Web Service to be called from script,
using ASP.NET AJAX, uncomment the following line.
// System.Web.Script.Services.ScriptService pub
lic class serverInfo System.Web.Services.WebServ
ice public serverInfo ()
//Uncomment the following line if using designed
components //InitializeComponent()
WebMethod(Description"This method
returns a greeting and the time of day.")
public string whatTime()
System.Net.NetworkInformation.IPGlobalProperties
thisServer System.Net.NetworkInformation.IPGlo
balProperties.GetIPGlobalProperties()
return "Good day mate! - It is "
DateTime.Now.ToLongTimeString() "
at host " thisServer.HostName "
(domain " thisServer.DomainName ")"
14Web Service Class
- The new class created derives from the class
System.Web.Services.WebService - This allows you to use ASP.Net objects such as
Application, Session, Server and User
15Documenting a Web Service
- Documentation (comments) is extremely important
as it tells subscribers how to use your Service - WebService declares a namespace, which you
choose, and includes a Description for the
Service - The namespace should include a reference to your
server and you, i.e. Seneca.Ian.Webservices - WebMethod includes a Description for each method
and must precede each method
16Testing Your Service
- Enter the URL of your service into a browser and
it will display the Service information page, if
your code is properly constructed
17- Select Service Description and you will see the
XML code for the Service - Select a Method name and you will see the XML
code for the Method when you are working on the
server - If you are working on the Local Machine you will
see a test page, click the test button and you
will see the results of a call to the Method