Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

XML Web Services: Just Another Data Tier? : Page 2

Many developers build a Web site and later, add XML Web services as separate projects, resulting in two code bases. This article shows you how to reuse XML Web service to provide data to your own applications.


advertisement
Create a Web Service
When you create an XML Web service, you make the service publicly available by creating a file with the extension asmx. For example, the sample project contains a Web service file called api.asmx. The asmx files themselves usually contain little code, just a reference to a code-behind module and a class in that module. In the sample project the code-behind class is called "api."

When you create a new Web service in Visual Studio, the Web service template generates the asmx file and a code-behind module automatically. The code-behind module contains a sample function decorated with the <WebMethod()> attribute and named HelloWorld . The function simply returns a string.

<WebMethod()> Public Function HelloWorld() As String return "Hello World" End Function

There have been a number of good articles on DevX about using attributes, so I won't repeat them here; it's sufficient to say that the WebMethod attribute instructs the ASP.NET runtime to build a SOAP interface around the class and also causes it to generate the sample pages seen when you navigate to the Web service in your browser. You can find the HelloWorld function in the api class in the sample project. The api class inherits from System.Web.Services.WebService.

The point to note here is that adding the WebMethod attribute to a method doesn't prevent you from using it in other ways. For example, you can still call the HelloWorld method from your own Web pages in the usual way—by creating an instance of the api class, calling the function, and assigning the return value to a server-side label. If you have downloaded the sample project and set the startup document to default.aspx, compile and run the project to see the results, which look like Figure 2.

Figure 2: The default.aspx file looks like this when loaded into a browser.
To provide some benchmarks, the page contains some quick and dirty performance counter code that displays the elapsed times for the method calls.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date