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.
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:
- MyFood()
- 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
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.
Finally click on the "Publish" Button.
After Success you will see this screen.
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"
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".
http://localhost/Myapptest/Service1.asmx now you can access this web service using this URL.