Custom Search

ASP.NET Web Services

This tutorial will explain what a web service is and how to setup a running example. A web service is a web application, consisted of a class that can invoke a method of another application. It utilizes the same structure of an ASP.NET application which includes using code behind.

Please note: You can hover your mouse over any of the images to enlarge the view.

Setup a new project

To get started creating your very first Web Services application, we will create a project. In this example I will be using Visual Studio Ultimate 2013. You can click on New Project under the Start category on the left or select the File Menu and Select New Project. Finally the shortcut keys used are Control + Shift + N.

master page

Creating your first Web Service

Next a popup dialog for New Project appears. Be sure that the Installed category on the left has Visual C# - Web - Visual Studio 2012 selected. Then you will select ASP.NET Empty Web Application in the center. You can change the frame work from .NET Framework 4.5 to .NET Framework 4.0 or that of your preference, but we will be using the .NET 4.5 Framework in this example.

In the Name window where it shows WebApplication1, you can rename this to anything you want. You can also change the project Location in the next field, using the Browse button to locate that specific directory. The Solution name will be altered as you modify the Name field. When you are satisifed with your results, click on the OK button.

master page

Visual Studio will then get to work building the solution files and everything necessary to get your first project ready. You may experience a slight delay as seen in the screenshot below.

master page

Visual Studio Empty Project

After the building process is complete you will see a blank screen toward the center. Notice there is a Toolbox category aligned vertically on the left. To the right you will see the Solution Explorer window. This area is where you will add new files, images, etc to the project. Below this you will see the Solution Explorer, Team Explorer, and Server Explorer tabs.

Below that you will see the Properties window. This section will manage most of the maintenance with forms, controls, and everything else that you will experience while using Visual Studio.

master page

Creating the Web Form

Next right click on your project (my example is called WebApplication1" and select Add - Existing Item. The shortcut is Shift + Alt + A. Then the Add New Item dialog box appears. Click on Web Form. You can rename this to your preference. I will be leaving the default set to WebForm1.aspx for simplicity. Click on the Add button when you are ready to build the form.

master page

You will now see some source code for the web form page just created. This will look very similiar to html. We will go over this more in a minute. Notice at the bottom of this page there are tabs for Design, Split, and Source.

master page

Click on the Design tab. This section is where you can add controls to the form. Controls are the textboxes, labels, buttons, drop down boxes, etc that you can see on actual websites. You will see the screenshot below.

master page

Please note: This section will not explain how the controls work, but you can see my other ASP.NET Tutorials for this. We will need to drag a textbox, button, literals, and some labels to our screen. Click on the Toolbox tab on the left. Under the Standard category, click and drag a TextBox to the white screen. Then drag a Button next to it. Press enter once after clicking to the right of the Button control. This will move the cursor to the next line. Finally you will complete the form by dragging three Labels on the screen. When you are done it should look something similiar to the screenshot below.

master page

Then click on the first Label you dragged on the screen. In the Properties window, erase the Text words "Label" with empty spaces. Do this for all three Labels. When you are done the screen should now look like this. This will prevent the word Label from being displayed on the screen when the application is run later in this tutorial.

master page

Next click on the Button control. In the Properties window change the Text section to Submit. Now click on the Source tab at the bottom again. You will see the Source view again and notice that code was added to this area when you dragged the controls to the screen earlier.

master page

Add the Web Service

After this click on the Project name again (my example is called WebApplication1). Then select Add - New Item. The Add New Item dialog box appears again. Make sure that the Web category is selected on the left and scroll down the center section until you see Web Service (ASMX). Click on this. The Name field changes to WebService1.asmx. Once again you are free to rename this, but we will be using the default name. The screenshot should be similiar to what is seen below.

master page

After this you will see the asmx page. It is here where all the main processing will take place once we run the project later. For now a few explanations are in order to understand what you are seeing on the screen.

master page

Within this code you will see the section [WebService(Namespace)] = "http://tempuri.org/")]. This section points to a namespace "http://tempuri.org" that will be unique among many other web services applications found on the Internet. The goal here is to change the url to something different or your own domain name (for your website or blog). This will allow no duplication of the service you are working on now. Everything below this section is the attribute settings for your web service. For now this will not be discussed.

However look at the method for WebService1. First you will see System.Web.Services.WebService. This allows the web service to be recognized from the namespace System.Web.Services. This class creates a simple HelloWorld that we will run soon to demonstrate finally what the application is supposed to do. The method also returns a string called "Hello, World".

Running the Web Service

We are going to see an example of what this Web Service looks like when it is run without making any changes to the namespace yet.

master page

To see the Web Service that was created by default change the url to your own domain in the WebService1.asmx.cs file. I have changed it to show [WebService(Namespace)] = "http://programmermind.com/)]". Now when you run the project again (use the menu DEBUG - Start Debugging) or just click on the green arrow at the top which lists the browser name to the right. Once it is rerun you will notice that the output has changed.

master page

Click on the HelloWorld link in the browser. You will now be able to invoke the Web Service. As an example if you were running a Web Service across the Internet from another domain you will see that application executed in your web browser.

master page

Now comes the moment you built this application for. Click on the button that says Invoke to see this Web Service live on the Internet. You will see it executed and displayed within XML code so that it can be recognized by the browser as http output.

master page

For the final example we are going to add a Service Reference to invoke a method that displays weather information based on a zip code you entered.

Add the Web Service Reference

Now right click on your solution name (my example is called WebApplication1). Select Add - Service Reference. Enter the url http://wsf.cdyne.com/WeatherWS/Weather.asmx and click the Go button. If you are successful you will see that Visual Studio searched across the Internet and found the Weather.asmx page at the website http://wsf.cdyne.com. You can either leave the Namespace as "ServiceReference1" or change it.

master page

Next click inside the window that says Services on the Weather category. This is in essense a class that is displaying on another web server. You will see three methods named, WeatherHttpGet, WeatherHttpPost, and WeatherSoap. Once you are satisifed with the name click on the OK button to add it to your project. It will now appear in a subdirectory called Service References.

master page

Adding the service reference for the Weather application

This Web Service was created by another company, but we can still run it. First though we will need to create some code to see the output of the methods. Switch over to the WebForm1.aspx.cs file by clicking on the tab at the top. You will now begin adding some code to implement this weather service in the Button_Click method. For easier reference I have already included the full source code below.

master page

Run the Web Service from another Provider

When you run the application you will see that it shows the textbox and button you created earlier. Now enter a zip code. I have used a zip code from my original home town.

master page

After clicking the Submit button you will see the results for the weather data displayed (where the Labels were added at earlier).

master page

Visual Studio Web Service Conclusion

This is the end of the tutorial for Web Services. I highly encourage you to begin writing your own Web Services and searching the Internet to see what type of Namespaces exist and what type of methods you can invoke. I hope you enjoyed this tutorial and encourage you to quick out my other C# examples, ASP.NET tutorials, SQL, etc.

Subscribe to the my newsletter
Privacy Notice: Programmer Mind will not compromise your privacy