Create Simple Web Service in Visual Studio 2008 / 2010 /2012

This tutorial explains how to create simple Web Services using Visual Studio 2008 or Visual Studio 2012.

How to create a simple Web service
 
When creating a New Project, under the language of your choice, select "Web" and then change to .NET Framework 3.5 and you will get the option to create an ASP.NET WEB Service Application.

WEB Service Application
 
I am naming my WEB Service as Myservice.
 
Now you can see the "Service1.asmx.cs" file and also a "[WebMethod] HelloWorld()" in it.

WEB Service Method
 
I am writing the following 2 simple WebMethod names:

  1. MyFood()
  2. CheckOddandEvenMethods()

WEB Service Method name

Then you can run your code and you can see the resulting page as below.
 
Service1.asmx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Services;

 

namespace Myservice

{

    [WebService(Namespace = "http://tempuri.org/")]

    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    [System.ComponentModel.ToolboxItem(false)]

    public class Service1 : System.Web.Services.WebService

    {

        [WebMethod]

        public string HelloWorld()

        {

            return "Hello World";

        }

        [WebMethod]

        public string MyFood(string items)

        {

            return "I like to eat" + items;

        }

 

        [WebMethod]

        public string CheckOddandEvenMethods(int a)

        {

            string results;

            if (a % 2 == 0)

            {

                return results = a + "_" + "Is a Even Number";

            }

            else

            {

                return results = a + "_" + "Is a odd Number";

            }

        }

    }

}
 
Service

Create the Client Program

Create Client Program
 
Now you need to add a Service Reference so that you can access your web service.

add Service Reference

Put Service Reference
 
Then click on the "Advanced" button below in the left corner.
 
Then you will see a screen like this will appear.

Service Reference Setting

Then click on "Add Web Reference" on the following left corner of the screen.
 
After clicking "Like" the following screen will appear:

Can Change Web reference Name

Here you need to give the URL of the web service we created earlier. As I said previously, the web service application that was created should be running in another instant of Visual Studio.

URL of the web service

Copy this URL and paste it into the following as you see.

add Service Reference URL

Then click on the "Go" button (->).

See methods

Then you will see methods that you have written in it.

Can Change Web reference Name
 
You can change a Web Reference Name here. (As you can see below.)

Solution Explorer Screen
 
After this click on "Add Reference".

The following screen will appear:

Solution Explorer

Then  open  "Program.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;

 

using ClientAccess.Mytestapp; // Here you need to write namespace Name and its WebReference Name //

 

namespace ClientAccess

{

    class Program

    {

        static void Main(string[] args)

        {

            // To call Webservice Here

            Service1 Myapp = new Service1();

            string Result1 = Myapp.MyFood("pav bhaji"); // Method 1

            string Result2 = Myapp.CheckOddandEvenMethods(10); // Method 2

            Console.WriteLine(Result1);

            Console.WriteLine(Result2);

            Console.ReadLine();

        }

    }

} 

  • First run your Service (Myservice)
  • Second run your Clientaccess (ClientAccess)

Final Output

Run your Service

Publish Our Web Service in Internet Information Service
 
Let's see how to publish our web service in IIS. Otherwise you always need to run your web service application in a separate Visual Studio instance. There first stop the web service application and go to the Solution Explore and right-click on the project. Then select "Publish".

Do as shown in the followiing this screen shots.

Publish Service

Publish Service url

Publish Web Service

Publish Web Service Connection

Publish Web Service Setting

Finally click on the "Publish" Button.

After Success you will see this screen.

Service Created

Now enable IIS in your computer and open the IIS Manager. I'm going to add my service to the Default Web Site. There right-click on the "Default Web Site" and click "Add Application"

IIS

There you will get the following window. Now you can provide an appropriate Alias (I have used "testservice") and select the physical path of your application. There you can provide the path to the folder we copied previously as in the following figure and click "Ok".

add application in IIS
 
http://localhost/Myapptest/Service1.asmx now you can access this web service using this URL.

Up Next
    Ebook Download
    View all
    Learn
    View all