Building Web Services with Visual Studio 2005 is incredibly easy. Microsoft has made it a cakewalk to put together a new Web Service application and expose methods off that Web Service.
To get started, create an ASP.NET Web Service application. Find this option by clicking File New Web Site in Visual Studio. From there, Visual Studio will ask you for the location of the Web server. Enter this as C:\Documents and Settings\Bill\My Documents\Visual Studio 2005\WebSites\HelloWordExample.
Unlike an ASP.NET Web Application project, Visual Studio creates an .asmx file, rather than an .aspx file. The .asmx file extension is short for Active Server Methods, derived from the fact that it contains methods that will be exposed through the Web Service.
By default, Visual Studio creates the Web Service using the code-behind model for the Web Service page. In addition to the .asmx file, Visual Studio also creates a Service.vb file and places this file in the App_Code folder of the project.
Open the Service.asmx file in Visual Studio. It contains only the WebService page directive, as shown here:
<%@ WebService Language="VB" CodeBehind="~/App_Code/Service.vb" %>
You use the @WebService directive instead of the @Page directive. The simple WebService directive has only four possible attributes:
Class - Required. It specifies the class used to define the methods and datatypes visible to the XML Web Service clients.
CodeBehind - Required only when you are working with an XML Web Service file using the code-behind model. It enables you to work with Web Services in two separate and more manageable pieces instead of a single file. The CodeBehind attribute takes a string value that represents the physical location of the second piece of the Web Service - the class file containing all the Web Service logic. In ASP.NET 2.0, it is best to place the code-behind files in the App_Code folder, starting with the default Web Service created by Visual Studio when you initially opened the Web Service project.
Debug - Optional. It takes a setting of either True or False. If the Debug attribute is set to True, the XML Web Service is compiled with debug symbols in place; setting the value to False ensures that the Web Service is compiled without the debug symbols in place.
Language - Required. It specifies the language used for the Web Service.
Instead of focusing on the Service.asmx page, double-click on the Service.vb file to open the file in the document window of Visual Studio. With the Service.vb file in the document window, notice that the single method on the page is decorated with the <WebMethod()> attribute. This attribute (System.Web.Services.WebMethodAttribute) is used to tell ASP.NET to expose this particular method through the Web Service.
Directly after the WebServiceBinding attribute, place the WebService attribute in code to define a custom namespace, which the industry recommends you always provide. The value of the namespace can be whatever you see fit; it doesn’t have to be an actual URL, just a unique identifier:
Imports System.Web Imports System.Web.Services Imports System.Web.Services.Protocols <WebService(Namespace:="http://localhost/HelloWorldExample")> _ <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _ <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _ Public Class Service Inherits System.Web.Services.WebService <WebMethod()> _ Public Function HelloWorld() As String Return "Hello World" End Function End Class
Now add a new method called GoodbyeWorld, without a WebMethod attribute:
Public Function GoodbyeWorld() As String Return "Goodbye World" End Function
Run the project. Visual Studio will open the Service.asmx file. By default, Web Services display a test interface (see Figure 26-3) that enables you to see which methods are available and execute them.
Figure 26-3
Notice that only the HelloWorld() method is displayed. This is the only method decorated with the WebMethod attribute; and hence the reason why GoodbyeWorld() and all of the inherited methods on the Service class were not displayed. Clicking the link enables you to invoke the method, as shown in Figure 26-4.
Figure 26-4
If you do this, the URL http://localhost:##/HelloWorldExample/Service.asmx/HelloWorld? is requested, which happens to be the URL for this specific method (running with the built-in Web Server provided with Visual Studio). You’ll then see the payload of the SOAP document directly in the browser, which contains the results of the call, as illustrated in Figure 26-5.
Figure 26-5
That’s pretty much all there is to Web Services from an implementation perspective when working with the .NET Framework. The .NET Framework deals with all of the plumbing (SOAP, WSDL, and so on) discussed in the first part of this chapter on your behalf, so you only have to add properly decorated methods to the service.