Microsoft & .NETASPBuilding Web Services with Visual Studio .NET

Building Web Services with Visual Studio .NET

In a real world scenario, applications have to get information from other programs for various purposes. These programs may be located on the local system, on an Intranet or on the Internet.

Java introduced Remote Method Invocation (RMI) and CORBA for achieving these types of tasks. With RMI, developers can call methods in a remote system. With the help of CORBA, developers can call methods running in a totally different operating system across networks. The main difficulty involved with these technologies is that developers have to write lot of source code for invoking the required methods. Moreover, there is no visual environment available for coding RMI and CORBA. These difficulties were completely eliminated with the introduction of Web Services in .NET Framework.

The main concept behind Web Services is that other applications can consume and invoke them. They can call methods and functions from any system over the Web through HTTP. They combine the power of COM and DCOM and generate SOAP by default. SOAP stands for Simple Object Access Protocol and it is used by the .NET Framework to call the appropriate Web Service via eXtensible Markup Language (XML). The end result of a .NET Web Service by default will be XML; however, you can build a GUI application to handle the services. It can be either a Windows Application (WinForm) or a Web Application (ASP.NET)

The main point to note is that Web Services don’t have user-interfaces (GUI controls)—the invoking application will have the required visual interfaces. You don’t have to learn anything about SOAP and XML to create Web Services. Instead, the .NET Framework handles the entire work for you. Moreover, Microsoft’s flagship product, Visual Studio .NET further simplifies the development of Web Services. In this article, you will learn how to create and consume Web Services with Visual Studio .NET. In order to work out the steps outlined in this article, you should install Visual Studio .NET under Windows 2000 Professional, Windows XP Professional, or Windows 2003 Server. The steps given below may slightly differ if you use standard editions of Visual Basic .NET and Visual C# .NET.

Creating a Web Service

Before consuming or applying a .NET Web Service, you should create one. First of all, launch Visual Studio .NET. I used Visual Studio .NET 2003 for writing this article. You can also use the 2002 edition. Before proceeding further, you should uncheck the Work Offline item on the Internet Explorer’s File Menu. Otherwise, Visual Studio .NET won’t be able to locate the Web server.

Select File | New | Project… to start a new Web Service Application. The New Project dialog opens up as shown in figure 1.

Figure 1: Visual Studio .NET New Project dialog box

You should choose ASP.NET Web Service from the templates section on the right side of the above dialog. Change the folder name (last part) of the location if required. Be sure not to modify the http://localhost from the location box. Upon clicking the OK button a new folder with the name, WebService1, will be created under Inetpub/wwwroot directory and you will be presented with the IDE as shown in figure 2.

Figure 2: Web Service IDE

As you may already know, Visual Studio .NET automatically creates lot of additional files (Open the WebService1 folder inside the wwwroot directory). These files (particularly Web.Config) are required for the Web Service to function properly.

Note – If you are moving forward to learn advanced Web Services development, you have to know how to add and modify the settings in Web.Config file. This file is widely used for storing the settings of a database like connection strings, passwords and other relevant particulars. A complete discussion about Web.Config file is beyond the scope of this article.

The next step is to open the code window by clicking the link titled “click here to switch to code view” from the above page. You can also double click anywhere on the page. Keep in mind that .NET Web Services have the file extension .asmx. Refer to the appendix given at the end of this article for a list of file extensions used in .NET. You will notice that Visual Studio .NET automatically creates a sample Web Service method named “HelloWorld()”.You can test this Web Service by uncommenting the lines and pressing F5. For our discussion, I am using another function named sum. The function accepts two integer parameters and returns their sum as the result. Enter the following code after either commenting the default function or deleting it:

  <WebMethod()> Public Function Sum(ByVal valueone As Integer, ByVal _ 
          valuetwo As Integer) As Integer
     sum = Val(valueone.ToString) + Val(valuetwo.ToString)
  End Function

All Web Service functions begin with the WebMethod tag. This is compulsory without which the function will not expose during runtime. Now you are ready to test the Web Service. After a series of build statements, you can be able to view the output as shown in Figure 3.

Figure 3: Web Service output

Note that Sum is the function name which I used above. As I explained above, if you are not including the WebMethod tag you cannot be able to view the function name. If you click the function name you will be presented with a page as shown in Figure 4.

Figure 4: Web Service Output

Since our function has two arguments, the Web Service automatically creates two text boxes with the names of the parameters which I used in the function. Also note that the output also shows SOAP codes. Now enter two integer values and press the Invoke button. You will be presented with the final output in a separate page as shown in Figure 5.

Figure 5: Final output of the Web Service

From the above screenshot, you will notice that the default output of our Web
Service is in XML format. The next step is to apply the above Web Service in a .NET application. In other words, some other application should consume this service. It can be applied either in a Visual Basic .NET or Visual C# .NET application. In the next section, you will learn how to apply the above Web Service in Visual Basic .NET.

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.

If everything goes on well, you should view the function name which I used above on the left side as shown in Figure 6. Finally, you have to add the reference to your WinForm application by clicking the Add Reference button.

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

 

Extension Name Description
.aspx ASP.NET Server Page
.asmx Web Service
.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 ananddotnet@yahoo.co.in

# # #

Get the Free Newsletter!
Subscribe to Developer Insider for top news, trends & analysis
This email address is invalid.
Get the Free Newsletter!
Subscribe to Developer Insider for top news, trends & analysis
This email address is invalid.

Latest Posts

Related Stories