How to build basic web service with ASP.NET in VB.NET- step 3

You can use the article How to build basic web service with ASP.NET – step 2 to verify that the class developed by you is ready for the Web. You will learn from this article how to convert it to a web service. In this case the BookService class   described in the article How to build basic web service with ASP.NET in VB.NET – step 1 is used. The very important first step is to add the System.Web.Services.WebMethod attribute to each method you want to expose as part of your wen service. This web service instructs ASP.NET to make this method available for inspection and remote invocation.

After these modifications class is looks like:


Public Class BooksService

<WebMethod( )> _

Public Function GetBooksCount() as Integer


End Function

<WebMethod( )> _

Public Function GetBooks() as DataSet


End Function

End Class



These two simple changes complete the transformation from your class into a web service. But, the client still has no entry point into your web service—i.e., there’s no way for another application to trigger your web methods. To allow this, you need to create an .asmx file that exposes the web service. ASP.NET implements web services as files with the .asmx extension. As with a web page, you can place the code for a web service directly in the .asmx or in a class in a code-behind file that the .asmx file references (which is the Visual Studio approach).

For example, you could create a file named BooksService.asmx and link it to your BooksService class. Every .asmx file begins with a WebService directive that declares the serverside language used in the file and the class. It can optionally declare other information, such as the code-behind file and whether you want to generate debug symbols during the compilation. In this respect it is similar to the Page directive for .aspx files. Here’s an example .asmx file with the BooksService:

<%@ WebService Language=”VB” %>

In this case, you have two choices:

– You can insert the class code immediately after the Web Service attribute

– You can compile it into one of the assemblies in the Bin directory.

If you add the BooksService class to a Visual Studio project, you can automatically compile it as part of the web application DLL, so you don’t need to include anything else in the .asmx file.

At this point, you’re finished. Your web service is complete, available, and ready to be used in other applications.