17.4 Aliasing Web Method Names

 <  Day Day Up  >  

You want to create an alias for a Web method to avoid naming collisions.


Technique

If two methods within a class that implements a Web Service use the same method name , add the MessageName parameter to the WebMethod attribute:

 
 [WebMethod( Description="Generates a random number within a range",     MessageName="GenerateNumberInRange" )] public int GenerateNumber( int Min, int Max ) {     Random rand = new Random( DateTime.Now.Millisecond );     return rand.Next( Min, Max ); } [WebMethod( Description="Generates a random number",     MessageName="GenerateNumber" )] public int GenerateNumber() {     Random rand = new Random( DateTime.Now.Millisecond );     return rand.Next(); } 

Comments

It was mentioned that ASP.NET generates the .asmx file at runtime as well as the WSDL file when requested . It does so by using reflection, which allows you to inspect type information of an object within a .NET assembly as well as extract any attributes applied to that data type. One side effect of this method, which works well, is the inability to detect errors at compile time. For example, if you create a Web method for a Web Service that returns a Hashtable object, your assembly will compile just fine. However, once you run the .asmx file within the browser, an exception is thrown, indicating that the Hashtable is an IDictionary -based collection, which is not a supported XML schema type. Likewise, if you create two Web methods whose name is the same and they differ only in their parameters or return types, compilation will be successful. When you run the .asmx file, an exception is thrown, indicating that two messages have the same name. If you recall from the last recipe, WSDL documents have a series of message elements for each request and response, which means having two methods with the same name invalidates that portion of the WSDL XML document. Method overloading is fine as long as you ensure that you change the message name if you are going to use those methods within a 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