17.2 Adding Web Service Methods

 <  Day Day Up  >  

17.2 Adding Web Service Methods

You want to add methods that clients can use to access the Web Service.


Technique

Adding a new method to a Web Service uses the same procedures that adding a method to a class uses. In other words, you can use the Add Method Wizard to add new methods or use the code view to manually add each method to the class. The difference is that a WebMethod attribute is applied to the method for ASP.NET to delineate between regular class methods and those exposed by the Web Service. Some of the parameters for this attribute are discussed in following recipes. The Lottery Web Service creates a Web Service method that returns an array of random integers. The parameters let you specify how many numbers to generate, the minimum and maximum values, and whether or not to include duplicates, as shown in Listing 17.3.

Listing 17.3 Creating a Web Service Method
 [WebMethod( Description="Generates a list of numbers between                          Min and Max with or without duplicates" )] public int[] GenerateNumbers(int Count, int Min, int Max, bool AllowDuplicates) {     Random rand = new Random( DateTime.Now.Millisecond );     ArrayList numbers = new ArrayList( Count );     for( int i = 0; i < Count; i++ )     {         int newNum = rand.Next( Min, Max );         if( AllowDuplicates == false )         {             while( numbers.Contains( newNum ) == true )             {                 newNum = rand.Next( Min, Max );             }         }         numbers.Add( newNum );     }     return (int[]) numbers.ToArray(typeof(int)); } 

Comments

After you add a new method to a Web Service, you have the ability to test it immediately without having to write any code that accesses the Web Service. The .asmx file in the last recipe simply contained a link to the WSDL file, but when you run a Web Service with a defined Web, an additional link is created to test the functionality of that method.

Once you click on the method name, you see the method name as well as TextBox controls for each parameter, allowing you to enter parameters to test the method. After you enter the necessary parameters, click the Invoke button to see the response. Assuming everything worked correctly, you see an XML document containing the response from your Web Service. If instead you see a file-not-found or other, similar error, then the method invocation failed within the method itself.

Web Service methods, unlike regular class methods, are limited to the types they can use for return values and parameters. Because Web Services are a multiplatform open standard, data types had to be standardized to accommodate all platforms. Most value types within the .NET framework are supported. You aren't, however, limited to just these types because composites of these types are also permitted. The example shown earlier returned an integer array, which is supported by Web Services. Also permitted are classes or structs that expose properties which also conform to these data types, allowing you to create custom Web Service data types without the risk of breaking any clients consuming the Web Service.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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