You can also create Web services using Visual Basic .NET. To build a Web service, create a new Visual Basic .NET project using the ASP.NET Web Service template, as shown in Figure 7.3. You'll need to specify the name of the Web service (in this case, StringProc) and the name of the server (in this case, HOURGLASS). Of course, you should use the name of your own Web server when creating a Web service project. Figure 7.3. Creating a new Web service with Visual Studio .NET.
The new project will contain a Web service class named Service1.asmx . You should rename this to represent the purpose of your Web servicefor example, Strings.asmx . You can then add code such as this to implement the Web service: Imports System.Web.Services <WebService(Namespace:="http://NetExam.org/StringProc")> _ Public Class Strings Inherits System.Web.Services.WebService <WebMethod()> Public Function ToUpper( _ ByVal inputString As String) _ As String ToUpper = inputString.ToUpper() End Function <WebMethod()> Public Function ToLower( _ ByVal inputString As String) _ As String ToLower = inputString.ToLower() End Function End Class Note the use of the WebService and WebMethod attributes in this code. The WebService attribute marks the Strings class as being a class that should be available to clients via SOAP. The WebMethod attribute is used to mark methods that can be called via the Web Services interface. To create the Web service, select Build, Build Solution in the Visual Studio .NET interface.
Testing the New Web ServiceTo ensure that a Web service performs as you expect, you should always test it. You may test the project by selecting Start from the Debug menu, which will open a browser window showing the test page for any exposed methods of the Web service, as shown in Figure 7.4. Figure 7.4. Exposed methods of the Strings Web service.
You may test any of the methods by clicking the method, which will open the Web method test page for that method, as shown in Figure 7.5. Figure 7.5. Test page for the ToUpper method.
Here, you may test the method by entering appropriate values for each parameter and clicking the Invoke button, which will open another browser window displaying the XML response (see Figure 7.6). Figure 7.6. The XML response from testing the ToUpper method.
|