Creating a Web Service with ASP.NET


<%@ WebService language="C#"    %> 

One of the web technologies with the highest growth rate nowadays is ASP.NET from Microsoft. One of the nice features of the technology is that creating and using Web Services is a rather easy task.

To implement a Web Service, a file with the extension .asmx must be created on the web server. Then, the <%@ WebService %> directive must be on top of the page. All methods that will be exposed as Web Services methods must be prefixed with the [WebMethod] attribute. Here is a complete example:

An ASP.NET Web Service (webservice.asmx)

<%@ WebService language="C#"    %> using System; using System.Web.Services; [WebService(Namespace="http://javascript.phrasebook.org/")] public class RandomNumberService {   [WebMethod]   public int randomNumber(int lower, int upper) {     Random r = new Random();     return r.Next(lower, upper + 1);   } } 

When you call this page from an ASP.NET-enabled web server in your web browser, you will get (depending on your configuration) a nice information page that gives you some details about the service. If you append ?WSDL to the URL, you get a WSDL description, automatically generated from ASP.NET. Figure 10.1 shows how that looks.

Figure 10.1. The automatically generated detail page for the ASP.NET Web Service.





JavaScript Phrasebook(c) Essential Code and Commands
JavaScript Phrasebook
ISBN: 0672328801
EAN: 2147483647
Year: 2006
Pages: 178

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