Technical Stuff · Web Services

How to run a .asmx web service without using Visual Studio?

Normally when we create a Web Service in Visual Studio , we just code the .asmx file and press the build button for successfully building and publishing the web service. After that , Web service is successfully running on your browser with URL  http://localhost/…/FirstService.asmx and then you can access the web service.

But, what if you want to do all this stuff  without using Visual Studio i.e. manually.I have gone through web and after wasting various hours of my life i get the solution.

To run a Web Service without Visual Studio you need following things:-


Based on the web service architecture, we create the following two components as a part of web services implementation:

Service Provider or Publisher

This is the provider of the web service. The service provider implements the service and makes it available on the Internet or intranet.

We will write and publish a simple web service using .NET SDK.

Service REQUESTER or Consumer

This is any consumer of the web service. The requester utilizes an existing web service by opening a network connection and sending an XML request.


Configuring the IIS

A web service can be published either on an intranet or the Internet. We will publish this web service on IIS running on a local machine. Let us start with configuring the IIS.

  • Open Start → Settings → Control Panel → Administrative tools → Internet Services Manager.
  • Expand and right-click on the default web site; select New → Virtual Directory. The Virtual Directory Creation Wizard opens. Click Next.
  • The “Virtual Directory Alias” screen opens. Type the virtual directory name. For example, MyWebServices. and click Next.
  • The “Web Site Content Directory” screen opens.
  • Enter the directory path name for the virtual directory. For example, c:\MyWebServices Click Next.
  • The “Access Permission” screen opens. Change the settings as per your requirements. Let us keep the default settings for this exercise.
  • Click the Next button. It completes the IIS configuration.
  • Click Finish to complete the configuration.

To test whether the IIS has been configured properly, copy an HTML file (For example, x.html) in the virtual directory (C:\MyWebServices) created above. Now, open Internet Explorer and type http://localhost/MyWebServices/x.html. It should open the x.html file.

Note: If it does not work, try replacing the localhost with the IP address of your machine. If it still does not work, check whether IIS is running; you may need to reconfigure the IIS and the Virtual Directory.


Creating .asmx file

Given below is our first web service example which works as a service provider and exposes two methods (add and SayHello) as the web services to be used by applications. This is a standard template for a web service. .NET web services use the .asmx extension. Note that a method exposed as a web service has the WebMethod attribute. Save this file as FirstService.asmx in the IIS virtual directory (as explained in configuring IIS; for example, c:\MyWebSerces).

FirstService.asmx

<%@ WebService language = "C#" class = "FirstService" %>

using System;
using System.Web.Services;
using System.Xml.Serialization;

[WebService(Namespace="http://localhost/MyWebServices/")]
public class FirstService : WebService
{
   [WebMethod]
   public int Add(int a, int b)
   {
      return a + b;
   }

   [WebMethod]
   public String SayHello()
   {
   return "Hello World";
   }
}

Testing the Web Service

  • To test this web service, copy FirstService.asmx in the IIS virtual directory created above (C:\MyWebServices).
  • Open the web service in Internet Explorer (http://localhost/MyWebServices/FirstService.asmx). It should open your web service page. The page should have links to two methods exposed as web services by our application.

Congratulations! You have written your first web service!

One thought on “How to run a .asmx web service without using Visual Studio?

Leave a comment