Building Web Services with Visual Studio .NET, Page 2
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.