Creating Web Services

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.

graphics/07fig03.jpg

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.

graphics/alert_icon.gif

When you build a Web service in Visual Studio .NET, the tools automatically take care of creating the appropriate WSDL file for the Web service.


Testing the New Web Service

To 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.

graphics/07fig04.jpg

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.

graphics/07fig05.jpg

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.

graphics/07fig06.jpg



Developing and Implementing WindowsR-based Applications with Visual BasicR. NET and Visual StudioR. NET Exam CramT 2 (Exam 70-306)
Developing and Implementing WindowsR-based Applications with Visual BasicR. NET and Visual StudioR. NET Exam CramT 2 (Exam 70-306)
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 188

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