How to Create a Web Service in C#

Category: XML and Web Services

How to Create a Web Service in C#

Introduction
In this article we will explain how you can easily create a web service within Visual Studio 2005.
Web Services are utilized in the design of SOA architecture as part of the foundation for the enterprise development of that architecture. SOA means Service Oriented Architecture.
In simplistic terms and using only one example of what web services are utilized for we are designing a layer between the front end [application] and the back end [database] that can be public facing and allow users with internet/network access to retrieve information from a database.
I mention internet and network in the sense that you can have a public facing web service that can be accessed via the internet and part of your web site for example. The Network mention refers to many corporations that have web services within their intranet that will return record sets of data or documents needed to maintain their daily operation.

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!


There are many facets to web services and the many ways they can benefit a corporation. But before we can do any of that we have to learn how to create a web service, and that is what we will learn in this article.

What we will learn in this article:

  • How to Create a Web Service

Getting Started
We will take you through the process of creating your first Web Service using Microsoft Visual Studio 2005 and C#. This will be a Web Service that we can consume in the future and we will during our next series of web service articles called “How to Consume a Web Service”. In that article we will show you how we can consume that Web Service utilizing .Net Web Service Studio and Microsoft Visual Studio.

Source Code for the Web Service
Start Visual Studio 2005 and create new Web Service project as illustrated in Fig. 1:

Fig. 1:


[Click to see full-size]


When you select the “OK” button the project will be created and the following code will be inserted into the file called “Service1.asmx.cs”. This is the main entry point for the web service.

 

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

namespace WebService1
{
///


/// Summary description for Service1
///

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{

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

 

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

This example code contains a “Public” method called “HelloWorld()” that returns the string “Hello World”. For simplicity purposes we will use this web service as is to show that you can create a web service and access it with ease. In future articles we can delve into the realm of modifying the web service and the nuances of doing so.
Next we select the “Start Application” button in “Debug” mode so we can run this web service and see how we can easily interact with it with no lengthy processes or third party applications required.

Fig. 2:

 

When the application is starting you will notice that it is initializing the “ASP.NET Development Server” and utilizing the local host service and assigning it a port. Fig. 3

Fig. 3


Once the web service has compiled and started you will be presented with the following Web Service screen.

 

Fig. 4:


[Click to see full-size]

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

This screen shows you the available public methods that are available within this web service. You will notice that the only method available in this web service is the “HelloWorld” method. This is because the method was declared as “Public” and not “Private”. If we had declared this method “Private” this same screen would be displayed but there would be no methods available for us to use externally.
If you click on the “HelloWorld” method you will be presented with the following screen. Fig. 5

Fig. 5:


[Click to see full-size]

 

This screen allows us to invoke the “HelloWorld” method and see what it will return. You will notice that we are inside that method within the web service and that there are sample SOAP examples for a request and a response that can be utilized to help you consume this web service.
In the next article in this series we will also show you another way of creating these objects dynamically and with great ease, which will offer even more functionality.
When you click on the “Invoke” button you will be presented with the following screen. Fig. 6

Fig. 6:


[Click to see full-size]


This screen displays the results returned from the “HelloWorld” web service we just created.

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

What we have Learned

We have learned that we can easily create a web service that can be used on a local intranet system or can be publicly accessed via the internet. We have learned how to initialize the web service and view the request and response objects associated with this web service. We also learned that when this web service is invoked it will return the string as designed.
Ideas for future enhancements:

 

  • Security
  • Encryption
  • Passing objects and parameters to the public method

Attachments



Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!