|
Visual Basic.NET (VB.NET) is also a part of Microsoft’s .NET initiative that supports Web Services. There are millions of developers who use VB 6, the predecessor to VB.NET, every day who are considering upgrading to .NET. This appendix examines creating Web Services and consumers using Visual Basic coding. Note that the download for the .NET Framework SDK mentioned in Appendix B does include a command line version of the VB.NET compiler.
Open Visual Studio.NET and create a new project. From the “New Project” dialogue, select “Visual Basic Projects” on the lefthand side and then on the right select “ASP.NET Web Service.” Figure D.1 shows the appropriate selections.
Figure D.1: The “New Project” dialogue in Visual Studio.NET with selections made for Visual Basic.NET.
The code Visual Studio.NET generates for you looks like the following.
Imports System.Web.Services <WebService(Namespace := "http://tempuri.org/")> _ Public Class XPlatform Inherits System.Web.Services.WebService #Region " Web Services Designer Generated Code " Public Sub New() MyBase.New() 'This call is required by the Web Services 'Designer. InitializeComponent() 'Add your own initialization code after the 'InitializeComponent() call End Sub 'Required by the Web Services Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the 'Web Services 'Designer 'It can be modified using the Web Services 'Designer. 'Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() components = New System.ComponentModel.Container() End Sub Protected Overloads Overrides Sub Dispose (ByVal disposing As Boolean) 'CODEGEN: This procedure is required by the Web 'Services Designer 'Do not modify it using the code editor. If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub #End Region ' WEB SERVICE EXAMPLE ' The HelloWorld() example service returns the string ' Hello World. ' To build, uncomment the following lines then save and ' build the project. ' To test this web service, ensure that the .asmx file ' is the start page ' and press F5. ' '<WebMethod()> Public Function HelloWorld() As String ' HelloWorld = "Hello World" ' End Function End Class
In the previous example, all the code that occurs before the #End Region is generated by Visual Studio.NET when you create the Web Service project. Much of this code calls many of the same objects as the code that gets generated when you create a Web Service in C#. Refer to Chapter 6 to see this code generated in a C# Web Service.
To add a method, remove the commented out hello world VS.NET example and add the following code.
<WebMethod(Description:="Simple Stock Example in " _ "VB.NET")> _ Public Function GetTestQuote(ByVal symbol As String)_ As Double Dim stockValue As Double = 55.95 Dim empty As Double = -1
If symbol = "C" Then Return (stockValue) Else Return (empty) End If End Function
Note | C# programmers resist the urge to use semicolons and open and closing brackets ({}) in the Visual Basic code. Visual Basic doesn’t have its roots in C and C++ like C#, so the syntax isn’t similar. Notice how the return type is last in the Function definition and how the if statement ends with an End If. This type of syntax has its roots in the Basic programming language, which the author programmed in as a kid on his Apple IIe. |
If you ignore the syntax of the Web Service, you’ll see some similarities. You still needed to define the function as a WebMethod and provide a signature for the function.
|