Service Development

Description Article
Table of Contents
Operations
Service Definition
How to do it

To develop a service using ASP.NET the following steps are required:

  1. Add to a separated file the complex class types sent on the service request and expected as service response (refer to Data Representation section);
  2. Add the WebService attribute to a class: If not specified in the attribute the web-service will be associated to the default namespace “http://tempuri.org“;
  3. Add the WebMethod attribute to each method of the class that are to be operations of the service

To develop a service using ASP.NET 2.0 or following versions the following steps are required:

  1. Add to a separated file the complex class types sent on the service request and expected as service response (refer to Data Representation section);
  2. Implement an interface with the required operations to be performed by the service;
  3. Add the WebService attribute to the interface: If not specified in the attribute the web-service will be associated to the default namespace “http://tempuri.org“;
  4. Add the WebMethod attribute to each method of (2);
  5. Add a class to implement the interface

Note: Using an interface it will constitute a contract for the operations performed by the service that can be reused with various classes that can implement that same contract in different ways.

Data Representation
How to do it

All the complex object data types required for parameterization of the invocation or expected as response of the requests must be marked with the Serializable attribute. This way the XMLSerializer class will automatically translate .NET objects into XML for transmission to and from the server.

Such classes can be written manually, or generated from definitions of the types in XML Schema using the command-line XML Schemas/Data Types Support Utility, “XSD.exe”.

Service Hosting
How to do it

The ASP.NET web services can only be hosted on the Internet Information Services (IIS) and therefore:

  • The web services are compiled into a class library assembly and then copied into the \bin subdirectory of that application root virtual directory
  • A file called the service file (one per web service) has the extension .”asmx” and contains the @ WebService directive that identifies the class that contains the code for the service and the assembly in which it is located:
<%@ WebService Language="C#" Class="ServiceFileName,ServiceAssemblyName" %>

and is copied into the ASP.NET application root virtual directory;

  • The configuration file is copied into the virtual directory and named “Web.config“;
  • The web service is then accessible by using the URL of the service file in the application root virtual directory.

If correctly hosted the broswer must responde to the URL with the Windows test page:

soap web service - how to invoke getCategory web method

Service Description
How to do it

Issuing an HTTP GET request for the .”asmx” file of an ASP.NET Web service with the query string “WSDL” causes ASP.NET to generate metadata and return the description of the service, in a WSDL format, as the response to the request:

ASP.NET request for web service description

Message Representation

The web service request and response are embebbed within a SOAP envelope.

Exception Handling
How to do it

Unhandled exceptions are returned to clients as SOAP faults.

If no custom error support mechanism is implemented  an exception can be handled as a SOAP fault and  explicitly raised through the SoapException class in order to abstract the complexities of the SOAP fault creation process.

Refer to this article for code details.