Start Window Service using .aspx page

Posted by Nismeh under ASP.NET on 11/22/2013 | Points: 10 | Views : 2165 | Status : [Member] | Replies : 6
Hi,

I want to start Window Service using a web page (.aspx) and also wants to pass an argument with it.

Can any one suggest me on that ?

IT KNOWLEDGE IS APPLIED KNOWLEDGE
So Just Do It



Responses

Posted by: Allemahesh on: 11/22/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
You can use the below methods for the same:-

Add the System.ServiceProcess reference form solution window.

using System.ServiceProcess;


public void StartService(string serviceName, int timeoutMilliseconds)

{
ServiceController service = new ServiceController(serviceName);

TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
if (Convert.ToString(service.Status) == "Stopped")
{
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
//"Service is Successfully Started.
}
//else
//Service is Already started
}


public void StopService(string serviceName, int timeoutMilliseconds)

{
ServiceController service = new ServiceController(serviceName);
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
if (Convert.ToString(service.Status) == "Running")
{
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
lblErrorMsg.Text = "Service is Successfully Stopped.";
}
//else
//Service is Already Stopped
}


public void RestartService(string serviceName, int timeoutMilliseconds)

{
ServiceController service = new ServiceController(serviceName);
int millisec1 = Environment.TickCount;
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
if (Convert.ToString(service.Status) == "Running")
{
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
//Service is Successfully Stopped.
// count the rest of the timeout
int millisec2 = Environment.TickCount;
timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2 - millisec1));
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
//Service is Successfully ReStarted.
}
//else
//Service is Currently Stopped. You Can't Re-Start the Service.
}


How to call the service form you page:-

StartService(_ServiceName, 30000);

StopService(_ServiceName, 30000);
RestartService(_ServiceName, 30000);


Happy Coding,
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Nismeh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 11/22/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Hi,
You have to first create window service, install it, then from services.msc-> you have to start service then in web application, using Process comes under System.Diagnostic name space, you can start service.

Nismeh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 11/22/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer
http://stackoverflow.com/questions/3512488/passing-parameters-in-windows-service

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Nismeh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nismeh on: 11/22/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Vishal - Neeraj,

I am doing same thing. I've created service in my project and now I am interested to execute it through my aspx page. Can you suggest further or if possible can I have snap of code so that I can refer it.

Thanks in Advance :)

IT KNOWLEDGE IS APPLIED KNOWLEDGE
So Just Do It

Nismeh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 11/22/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
You can refer below link
http://weblogs.asp.net/kaushal/archive/2011/02/28/start-stop-window-service-from-asp-net-page.aspx

Nismeh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 11/22/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
you can also refer -
http://www.fredmastro.com/post/Control-Your-Windows-Service-from-a-AspNET-Web-App-Start-Stop-Run-A-Method.aspx

Nismeh, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response