Thursday, August 31, 2006

Write a Simple Web Service .NET


You can write a simple XML Web service in a few minutes using any text editor. The service you will create in this section, MathService, exposes methods for adding, subtracting, dividing, and multiplying two numbers. At the top of the page, the following directive identifies the file as a XML Web service in addition to specifying the language for the service (C#, in this case).

<%@ WebService Language="C#" Class="MathService" %>

In this same file, you define a class that encapsulates the functionality of your service. This class should be public, and can optionally inherit from the WebService base class. Each method that will be exposed from the service is flagged with a [WebMethod] attribute in front of it. Without this attribute, the method will not be exposed from the service. This is sometimes useful for hiding implementation details called by public Web Service methods, or in the case where the WebService class is also used in local applications (a local application can use any public class, but only WebMethod classes are remotely accessible as XML Web services).

Imports SystemImports System.Web.ServicesPublic Class MathService : Inherits WebService   <WebMethod()> Public Function Add(a As Integer, b As Integer) As Integer       Return(a + b)   End FunctionEnd Class
VB 

XML Web service files are saved under the .asmx file extension. Like .aspx files, these are automatically compiled by the ASP.NET runtime when a request to the service is made (subsequent requests are serviced by a cached precompiled type object). In the case of MathService, you have defined the WebService class in the .asmx file itself. Note that if an .asmx file is requested by a browser, the ASP.NET runtime returns a XML Web service Help page that describes the Web Service.

Precompiled XML Web services

If you have a precompiled class that you want to expose as a XML Web service (and this class exposes methods marked with the [WebMethod] attribute), you can create an .asmx file with only the following line.

<%@ WebService Class="MyWebApplication.MyWebService" %>

MyWebApplication.MyWebService defines the WebService class, and is contained in the \bin subdirectory of the ASP.NET application.

Consuming a XML Web service from a Client Application

To consume this service, you need to use the Web Services Description Language command-line tool (WSDL.exe) included in the SDK to create a proxy class that is similar to the class defined in the .asmx file. (It will contain only the WebMethod methods.) Then, you compile your code with this proxy class included.

WSDL.exe accepts a variety of command-line options, however to create a proxy only one option is required: the URI to the WSDL. In this example, we are passing a few extra options that specify the preferred language, namespace, and output location for the proxy. We are also compiling against a previously saved WSDL file instead of the URI to the service itself:

wsdl.exe /l:CS /n:MathService /out:MathService.cs MathService.wsdl

Once the proxy class exists, you can create objects based on it. Each method call made with the object then goes out to the URI of the XML Web service (usually as a SOAP request).

For more information click here

Tags: , , , , , , , , , ,

0 Comments:

Post a Comment

<< Home