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

 

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.

check out my blog post

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.

check out my blog post

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

I am writing the following 2 simple WebMethod names:

  1. MyFood()
  2. CheckOddandEvenMethods()

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”;

}

}

}

}

Create the Client Program

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

Then click on the “Advanced” button below in the left corner.
 
Then you will see a screen like this will appear.

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

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.

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

Then click on the “Go” button (->).

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

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

After this click on “Add Reference”.

The following screen will appear:

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


Post a Comment

0 Comments