Understanding Web Services in C# 2.0

Although I am pretty much sure that there are enough articles and tutorials on similar subjects available over the internet by now, this article is targeted for beginners (I define a beginners as someone with an understanding of C# but does not have much hands-on experience) and I will try to keep the terminology as simple as possible for a better understanding.

The agenda of this article is to help explain the basics of Web Services. Though we are trending towards WCF, I strongly feel all my fellow developers out there who want to start WCF should understand the transformation of Web Services to WCF. There is no harm in learning before you leap, guys.

Let's start with the basic requiremetns of Web Services and how they are useful in the real world. All the statements and the use-cases I mention here may not be useful “as-is” but these are purely based on my experiences across various projects. So, no disclaimers are made. :).

Why Web Services

If you are a developer in an enterprise application development, then this question suits you very well. Usually in enterprise application development, there will be a huge chance that the inter-dependent components may not be built using the same language. For example, a Graphical User Interface (GUI) may be developed using Adobe, the back-end may be in .NET and the other components could be C++. Assume that your back-end should be a single point of contact for all the components and also the database.

So, how would you ensure that there is no notational problem while communicating the data across the components? The Web Service Concept provides you a platform to define the protocols that can be used across various components though all of them may not be in .NET, technically termed “Interoperability”.

Real-time Usage

Assume that you are doing an online shopping and you have used your credit/debit card to complete the transaction.

Upfront, you can see there should be three components to do this task:

  1. Credit / Debit Card payment interface (responsible for accepting your credit/debit card details and sending that over to the bank for verification).

  2. Credit / Debit Card Verification Service at the respective bank's end.

  3. Secure and Centralized Database at the respective bank's end.

The component #2 is the one which is very crucial for the transaction to be completed. In layman terms, this component should have the capability to receive requests from various payment gateways and should always have the continuous listener capability for all the incoming requests so that none of the requests are dropped. Also, the business logic should be encapsulated and be sure that the data security is tightened because it would have a direct access to the secure database. The specific component that can facilitate this requirement is a “Web Service”.

I hope the use-case mentioned above is clear, however I will be covering such real time examples going forward.

Setting up the development environment

Pre-requisites for development

  1. .NET Framework (2.0 or above).
  2. Microsoft Visual Studio 2005 or above with C# Development Environment Settings.
  3. SQL Server Express Edition 2005 or above.
  4. .NET Framework (2.0 or above).
  5. IIS 6.0 or above (IIS version 5.1 also is supported that usually ports with WinXP).
  6. SQL Server Express Edition 2005 or above.

Building your first .NET Web Service

Now, I assume you have the stage set ready for your actual development. Here onwards, I will try to provide the screen captures as much as possible for your easy understanding.

Open Visual Studio 2008 from Start -> Program Files -> Microsoft Visual Studio 2008.

Let's initially stick to the Visual Studio 2008 for this session and we can see how to create a Web Service using 2010 in the next session.

The first step is to create a .NET 2.0 Web Service as a Web application using File -> New -> Website.


The new file dialog box will be popped up asking you for the type of web application that must be created. As you can see on the page, there are various templates like ASP.NET Website. You should select the ASP.NET Web Service template to create a Web Service. Also provide the name of the Web Service solution in the text box provided to the right of the File System Drop Down. In our example, I have provided the name as “DemoWebService”.


Note: If you have multiple versions of .NET framework installed on your machine and you still want to develop a .NET framework 2.0 application, the Visual Studio IDE provides you the option to select a target framework.

Please refer to the following screen shot to change the target framework of your application in the New File dialog:

new file

Once this is done, you should have a solution named “Demowebservice” in your Visual Studio IDE and your page should look like this:

Demowebservice code

There are a couple of things that must be observed on this page. The Solution Explorer has a defined hierarchy with a structure to have the code behind files (.cs files), configuration file and XML files.
The first set is the “using” clause that imports the default namespace(s) required for the Web Service. You may also need a new using clause by adding a custom DLL as a reference DLL to the project.

The next statement says that the Web Service domain namespace is http://tempuri.org. This definition is required as a unique namespace so that there would be no conflict with other service schema. You are free feel to change this to a valid domain, before you deploy the Web Service.

Then you define your class name service1 by default and can be renamed as per the naming standards. The next important aspect is to define the service interface methods.

As you are aware, by definition, service methods can be defined as an interface method that will just have the method name, method signature (parameters) and method output type and also with the method implementation at the service layer itself.

As you can see in the preceding screenshot, a standard method is defined for your convenience with the name “HelloWorld” with implementation.

Please note that each method that must be exposed to your Web Service client should be marked with a (webmethod) attribute tag. Any public interface method without this tag will not be visible to your Web Service client.

Though the service has the capability to hold both the interface and the implementation, it is always advisable to maintain a separate code behind class that inherits the Web Service interface.

Compile/Debug your Web Service

Once the service definition is complete, build the application and when the compilation is successful, you may test the service by pressing the “F5” key or by navigating to the “Debug” menu and select “Start Debugging” as in my screen shot below.

start debugging

Test your Web Service:

If everything goes good, then you should be able to see a browser-based window that shows all the interfaces you have defined at your service level. In my example, it is the HelloWorld method that is displayed on my machine's default browser. (Refer to the screenshot below).

hello word

Now, to test your service method, click on the specific method name and you can see a new browser tab with an Invoke Button. (Refer to the screen shot below). Hit the invoke button to see the desired output. In my example, I am expecting a “hello world” message.


local host

Publish your Web Service

Now that you have tested your Web Service, we are ready to deploy the service. Always ensure that you need to deploy the published version of the service and the build binaries. As you are wondering, yes, there is a difference in both of the versions. A published version of any service will have only the compiled version of the binaries /app_code folder that essentially means that you are not giving away your code (.cs) files.

To publish your Web Service, you need to select your application at the service level. To do that right-click and click on Publish. In my example, I will select the DemoWebService project and do a right-click to publish.


You will be prompted with a publishing wizard that is a one -time activity to publish any service.

As in my screenshot below, select a new profile (if you don't have any existing profiles) and provide a name for your profile.


After providing a name to the service publish profile, you need to specify the type of deployment that includes a direct deployment to a web server, file copy or a FTP. In my example, I considered File System that means a local machine copy to the desire location.


The following screenshot refers to a file system publish method that I gave a folder name as webservicebins at my desktop location.

system publish method

The next step is to select the type of release that includes a debug or a release version.

I would recommend to select a release version if you deploy your Web Service to production and select the option to delete all the existing files before publishing. Note that the delete would happen on your local machine for a file system and on the webserver for a web deploy.


You are almost there to publish your Web Service. Click on the Publish button and you can see the list of binaries in your publish path.

publish path

Deployment of Web Service:

Now that we have the release binaries with you, we need a webserver to deploy the Web Service so that the service clients can begin  to be accessed. In our example, let's consider IIS as a webserver to deploy the Web Service. To access the webserver/IIS on your machine, go to Start -> Run and type INETMGR. If IIS is configured on your machine, you can see a result similar to the screen shot below.

Deployment of web service

If not installed, then do the following procedure to install IIS:

  1. Navigate to Control Panel and select Add/remove programs.
  2. Click on Turn windows features on/off .
  3. Select Internet Information Systems (IIS).
  4. Click OK.

    install IIS

If you have successfully installed and are able to access IIS, then you should be able to see the IIS Manager as in my screen shot below.

http request

To deploy the webservice, we need to create a new application by doing a right-click on the default website and select “Add Application” that will prompt for the application details as in the following.

Add Application

Provide a proper alias name (this name would be accessed by all your webservice clients), select the physical path where you have published your service binaries and click "OK". You should be able to view the application in your IIS and to test it. Right-click the application and click on browse (refer to the following screenshot).


If all is well, you should be able to see a browser tab listing all the methods in your webservice. Please do not forget to enable the directory browsing to see the list of all published files of your service.

I hope this article will help you to understand the basics of webservices, though we are trending towards WCF. I will try to explain the basics of WCF and all the advantages you carry with WCF in my next article.

Keep coding and keep smiling.

Up Next
    Ebook Download
    View all
    View all