HOST ASP.NET Web service in a Windows service

The following is the procedure to host a web service in windows service.

  1. Open Visual Studio 2005.
  2. Create an ASP.NET Web service.

          a. File menu, choose New Web Site

          b. In the New Web Site dialogue box, select the ASP.NET Web Service.

          c. Enter the address of web server and click Ok.

3. Add code to implement the Web Service.

4. Create a Windows service.

         a. File menu, choose New Project

         b. In the New Project dialogue box, select windows service.

         c. Specify the name and click Ok.

5. Add references to the Microsoft.Web.Services3, System.Web.Services, and System.Web assemblies.

6. Add source file and configuration file to the Windows service project.

    a. Right click the windows service project, point to Add choose Existing Item.

    b. Navigate to the configuration file in the web service folder select

       Web.config, and click Add.

    c. Right click the windows service project, point to Add choose Existing Item.

    d. Navigate to App_code folder web service project select the source file   

       Service1.cs, and click Add.

    e. Rename the Web.config file to app.config.

7. Open the source file in windows service project

    write the following code in OnStart Method

protected override void OnStart(string[] args)
    Uri address = new Uri("soap.tcp://localhost/TestService");
    SoapReceivers.Add(new EndpointReference(address), typeof(Service ));
8. Write the following code in OnStop Method.
protected override void OnStop()
 { SoapReceivers.Clear(); }
9.Write the following code in Main Method
static void Main()
    System.ServiceProcess.ServiceBase[] ServicesToRun;
    // Change the following line to match.
    ServicesToRun = new System.ServiceProcess.ServiceBase[] 
    { new WindowsServiceToHostASMXWebService() };

10. Install the Windows Service.

%d bloggers like this: