{infiniteZest}
// Articles. Tutorials. Utilities.
Home  |   Search  |   Login  
Categories Skip Navigation Links
New / All
AJAX
Apple
ASP.NET
.NET
Git
Google / Android
Python / IronPython
Miscellaneous
SQL Server
First Web Service Program with C# Explained
Summary
This article explains and discusses the creation of the first web service program using .NET Framework and C#.
 
Table of Contents

Creating a Web Service Project

Figure 1. New Web Service Project

Generated Files

Figure 2. Solution Explorer for the FirstWebService

FirstWebService.sln and FirstWebService.suo

FirstWebService.csproj and FirstWebService.csproj.user

Service1.asmx and Service1.asmx.cs

Web.config

AssemblyInfo.cs

Code Analysis

Service1.asmx

Code Listing 1. Service1.asmx

Service1.asmx.cs

Code Listing 2. Service1.asmx.cs

WebService

Code Listing 3. A Web Service

WebMethod

Code Listing 4. A Web Method

Running / Testing the Web Service

Figure 3. The web service and its methods

Figure 4. Request and Response for the HelloWorld web method

Code Listing 5. The response from the HelloWorld method

 
Article Series
Previous Article:
Test Your Knowledge: First Smart Device Program in C# Explained
This article is part of the Series:
Article Series: Advanced First Programs in C# Explained
Next Article:
Test Your Knowledge: First Web Service Program with C# Explained

Web Services are the programs without user interface. They are pieces of functionality that reside either on your site or some other site. You just call these methods with appropriate parameters and get back the results.

Creating a Web Service Project

In order to create a new Web Service project from Visual Studio 2008, choose File – New – Project, to open up the New Project window.

Figure 1. New Web Service Project

Figure 1. New Web Service Project

As shown in Figure 1 (New Project), the Web related functionality is branded as ASP.NET (you also have WCF Service Application). In order to create a web service, choose ASP.NET Web Service Application template.

Generated Files

When you click OK in the New Project dialog box, a new solution will be created for the FirstWebService web service. The Figure 2 below shows the Solution Explorer for this web service.

Figure 2. Solution Explorer for the FirstWebService

Figure 2. Solution Explorer for the FirstWebService

Let’s discuss some of the important files generated as a part of this solution.

FirstWebService.sln and FirstWebService.suo

These are the standard solution files that are generated as a part of every Visual Studio solution. The .sln file is text files that contains the metadata of the solution (including which projects are included in this solution). And .suo is a binary file that contains the Visual Studio user-specific (i.e. developer-specific) information like preferences, etc.

FirstWebService.csproj and FirstWebService.csproj.user

These are the Project files for the web service. The .csproj files are typically very detailed and contain a ton of information about the project. This information includes the files and references, compilation preferences, etc.

There is also a user-specific project file – with the extension csproj.user. This file contains information about how this web service is run.

Service1.asmx and Service1.asmx.cs

These are the main files where the web service you are going to develop will reside. In fact, typically, you will change only the asmx.cs file (you would add the web methods and additional logic to the web service).

The .asmx and .asmx.cs files are similar in concept to the .aspx and .aspx.cs file. However, since a web service do NOT have user interface, the .asmx file will be very light.

The code in these files is discussed in the section below.

Web.config

Except for the user interface, the web services can depend on the functionality available in ASP.NET. That means, it can make use of authentication, http handlers, http modules, databases (using the appropriate connection strings), etc.

The Web.config (as in the Web Application projects) stores all this configuration information.

AssemblyInfo.cs

This is a .NET related file (not a web service specific file). As the name says, this file contains the assembly (in this case a dll) related information. That would be the title, description, company, version, etc. of that assembly.

Code Analysis

Let’s look at two web service related code files that are generated as a part of this solution.

Service1.asmx

Code Listing 1. Service1.asmx

<%@ WebService Language="C#" CodeBehind="Service1.asmx.cs" Class="FirstWebService.Service1" %>

As mentioned above, .asmx file is similar to .aspx file in concept. However, as you can see in Code Listing 1, the .asmx file is very light because there is no user interface attached to a service.

This .asmx file has the basic information about this web service:

  • the code behind is in Service1.asmx.cs
  • the language of the code is C#
  • the class for this service is Service1 in the namespace FirstWebService

Service1.asmx.cs

Code Listing 2. Service1.asmx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace FirstWebService
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
}

In this asmx.cs file you will develop the web service. First thing you would do, of course, is to change the name Service1 to something that is more suitable to your service.

This piece of code introduces two important concepts: WebService and Method.

WebService

Code Listing 3. A Web Service

    [WebService(Namespace = "http://tempuri.org/")]
    public class Service1 : System.Web.Services.WebService
    {
    }

What ever the web service you write (in this case it is Service1), it would a class derived from .NET Framework’s WebService class in the System.Web.Services namespace.

You will say that a particular class is a web service and contains methods accessible from other locations on the Internet, by using the attribute [WebService]. This web service namespace (not the namespace of the class itself), is, by default, http://tempuri.org. You would want to change that to your company, for example, http://yourcompany.com.

WebMethod

Code Listing 4. A Web Method

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

A web service class (Service1, as shown in Code Listing 3), contains logic/code (methods, etc.), only some of which is accessible from other locations on the Internet. Meaning, in this class, you will have some web methods and some helper methods that do additional processing.

So, the HelloWorld() method shown in Code Listing 4 will be accessible from other sites on the Internet – because we used the [WebMethod] attribute on this method.

You can have other methods in this class without that attribute ([WebMethod]), and they will not be accessible.

Running / Testing the Web Service

When we run this web service (by pressing F5), the Visual Studio displays the methods available in this class with a default interface (this is not a user interface for the service – since web services don’t have user interfaces; but it’s just a friendly listing of the methods in this service and way to test them).

Figure 3. The web service and its methods

Figure 3. The web service and its methods

If you click on the Service Description the WSDL (Web Service Definition Language) of this web service. This is an XML file that conforms to the WSDL standard. So, whoever is getting this from the other side will be able to understand what this web service offers.

Underneath the service, you will see a list of web methods in this service (here you see just the HelloWorld, because that’s the only method in this service).

Figure 4. Request and Response for the HelloWorld web method

Figure 4. Request and Response for the HelloWorld web method

When you click on HelloWorld, you will be able to test this web method. On this page, you will also be able to see the request and response in SOAP 1.1, SOAP 1.2, and HTTP POST formats.

The HelloWorld method does not take any parameters; and as you can see from the Code Listing 4 for the method itself, it returns a string “Hello World” when called. So, we can test this by clicking on the Invoke button provided on this page.

Code Listing 5. The response from the HelloWorld method

  <?xml version="1.0" encoding="utf-8" ?>
  <string xmlns="http://tempuri.org/">Hello World</string>

When the HelloWorld method is called by some other site on the Internet, it will receive the XML shown in the Code Listing 5. As you can see, it contains a string Hello World.

Take a Quick Quiz on this Article

1. TRUE or FALSE: A typical ASP.NET Web Service Application has an elaborate user interface.



: [No Hint Available. You are on your own.]
Question 1 of 5
Article Series
Previous Article:
Test Your Knowledge: First Smart Device Program in C# Explained
This article is part of the Series:
Article Series: Advanced First Programs in C# Explained
Next Article:
Test Your Knowledge: First Web Service Program with C# Explained
Bookmark and Share This

More Articles With Similar Tags
This test has questions on developing basic web services with C# programming language.
This test has questions on the basics of developing an ASP.NET server control program with C# programming language.
Test your knowledge on the basics involved in creating the first ASP.NET Server Control program using C#.
How would you start with developing a Windows Mobile program using C# programming language?
icon-first-aspnet-server-control-solution-explorer.jpg
This article analyzes and explains the first ASP.NET Server Control. Server Controls have functionality and user interface that can be plugged into web application projects easily.
About  Contact  Privacy Policy  Site Map