.NET Web Services Support


Consider the SimpleCalculator web service, shown in Example A-1, which provides the four basic arithmetic operations.

Example A-1. The SimpleCalculator web service
 using System.Web.Services; [WebService(Namespace="http://CalculationServices",             Description = "The SimpleCalculator Web Service provides the                            four basic arithmetic operations for integers.")] public class SimpleCalculator {    [WebMethod]    public int Add(int argument1,int argument2)    {       return argument1 + argument2;    }    [WebMethod]    public int Subtract(int argument1,int argument2)    {       return argument1 - argument2;    }    [WebMethod]    public int Divide(int argument1,int argument2)    {       return argument1 / argument2;    }    [WebMethod]    public int Multiply(int argument1,int argument2)    {       return argument1 * argument2;    } }

Using .NET, all you have to do to develop a web service is add the WebMethod attribute to the methods you wish to expose as web services.NET will do the rest. The WebServiceAttribute attribute is optional, but you should use it. The attribute is defined as:

     [AttributeUsage(AttributeTargets.Class|AttributeTargets.Interface)]     public sealed class WebServiceAttribute : Attribute     {        public WebServiceAttribute(  );        public string Description{get; set;}        public string Name{get; set;}        public string Namespace{get; set;}     }

WebServiceAttribute lets you specify a web service namespace that contains your service, used like a normal .NET namespace to reduce collisions. If you don't specify a namespace, Visual Studio 2005 uses http://tempuri.org/ as a default. A published service uses a specific URI as its namespace. WebServiceAttribute also allows you to provide a free-text description of the service. The description appears in the auto-generated browser page used by the service consumers and testers during development.

Producing client code for a web service is equally trivial. Select Add Web Reference from the client's project in Visual Studio 2005 and point the wizard at the site containing the web service .aspx file (you can also select a web service from your solution or from your machine). Once the wizard presents the available web services, select the desired web service and click Add Reference. This causes Visual Studio 2005 to generate a proxy class that the client uses to invoke the web service. Example A-2 shows the proxy class for the service presented in Example A-1, with some of the code removed for clarity.

Example A-2. The SimpleCalculator web service proxy class
 public partial class SimpleCalculator : SoapHttpClientProtocol {    public SimpleCalculator(  )    {       Url = Settings.Default.<App name>_<Reference name>_SimpleCalculator;    }    [SoapDocumentMethod("http://CalculationServices/Add")]    public int Add(int argument1,int argument2)    {       object[] results = Invoke("Add",new object[]{argument1, argument2});       return (int)(results[0]);    }    //Other methods and properties }

The SimpleCalculator proxy class contains a public method for each of the methods exposed as web methods by the original web service. The proxy class (sometimes called a web service wrapper class) completely encapsulates the complex interaction with the remote service.

The proxy class provides the Url property, used to set and get the service's address. When adding a web reference in Visual Studio 2005, it will also add to Project's Settings class a property named according to this format:

     <App name>_<reference name>_<Service name>

and bind it to a matching entry in the application's configuration file, which contains the service's address. In addition, Visual Studio 2005 will decorate the property with the DefaultSettingValue attribute. The DefaultSettingValue attribute will contain the default address of the web service, in case the entry in the application configuration file is missing. The default address will simply be the originally referenced web address.

The client code can use the proxy class as if the SimpleCalculator object were a normal local object:

     SimpleCalculator calculator;     calculator = new SimpleCalculator(  );     int result = calculator.Add(2,3);     Debug.Assert(result == 5);



Programming. NET Components
Programming .NET Components, 2nd Edition
ISBN: 0596102070
EAN: 2147483647
Year: 2003
Pages: 145
Authors: Juval Lowy

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net