Building Web Services with Visual Studio .NET, Page 3
Applying the Web Service in a Windows Application
Start a new Visual Basic .NET project by selecting File | New | Project. You have to select Windows Application from the New Project dialog box. This is because of the fact that you are creating a Windows form application. This will create a new IDE with a single Form and other usual elements like ToolBox, Solution Explorer, etc.
Before adding controls to the Form from the Toolbox, you should add a reference of our Web Service to the Windows form application. For this purpose, select Add Web Reference from the Project menu. Click the link titled "Web Services on the local machine" from the Add Web Reference dialog box. This will display a list of all services running on your system. Select an appropriate service by verifying the URL.
Figure 6: Add Web Reference Window
The Solution Explorer window should now display the details of the Web Service. As a last step, create a simple GUI by adding two labels, two textboxes and a button. Double click the button and add the following code:
Dim testservice As New localhost.Service1() MessageBox.Show(testservice.sum(TextBox1.Text, TextBox2.Text))
From the above code, you can see that the variable testservice creates a new instance of our Web Service and the resulting output will be displayed on a message box. Visual Studio .NET automatically displays the relevant Web Service and the function names through its Intellisense system. You will also notice that the WinForm application does not contain any functions for calculating the sum. The Web Service created above does all the work.
Applying the Web Service in an ASP.NET application
You can also apply the above Web Service in an ASP.NET page. But instead of selecting Windows Application, you have to select ASP.NET Web Application from the templates section of the New Project dialog (See Figure 1). Further, you have to add one more Label control to the page in addition to two labels, two textboxes and a button. Add the following code by double clicking the button.
Dim testservice As New localhost.Service1 TextBox3.Text = testservice.sum(TextBox1.Text, TextBox2.Text)
The resulting output will be displayed in the newly added Label control. Keep in mind to change the foreground color of the Label control. Otherwise, you won't be able to view the output since the default color is white.
In this article, you learned how to create and consume a .NET Web service using Visual Studio .NET. If you don't have Visual Studio .NET, don't panic. I strongly recommend you to check out ASP.NET WebMatrix. This is a free product from Microsoft and can be downloaded from www.asp.net.
Appendix - .NET Extensions
|.aspx||ASP.NET Server Page|
|.ascx||ASP.NET Custom Control|
|.mspx||User defined extension for a ASP.NET page. It should be enabled on the IIS.|
About the Author
Anand Narayanaswamy, a Microsoft Most Valuable Professional, works as an independent Web/Software developer and technical writer. Anand runs learnxpress.com and specializes in ASP, ASP.NET, C#, Visual Basic 6.0, Visual Basic .NET and in the development of courseware, technical articles, documentation and reviews of products and books. He can be reached at firstname.lastname@example.org
# # #
Page 3 of 3