Skip to content

Instantly share code, notes, and snippets.

@arijusg
Created June 6, 2014 14:28
Show Gist options
  • Save arijusg/b6d70a030df4708f4173 to your computer and use it in GitHub Desktop.
Save arijusg/b6d70a030df4708f4173 to your computer and use it in GitHub Desktop.
Control Windows services from an ASP.Net WebApp
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.ServiceProcess;
namespace LiveUploaderManagementSystem.Services
{
public static class ServicesClass
{
private static DataTable _dtServices;
private static void GenerateServicesTable()
{
_dtServices = new DataTable();
_dtServices.Columns.Add("Name", typeof(string));
_dtServices.Columns.Add("Status", typeof(string));
}
public static DataTable GetServiceList()
{
GenerateServicesTable();
SelectDesiredServices();
return _dtServices;
}
private static void SelectDesiredServices()
{
var serviceControllers = ServiceController.GetServices();
var s2 = from s
in serviceControllers
where s.ServiceName == "LiveUploaderService" || s.ServiceName == "LiveUploaderJobAndInvoiceIdMergerService"
select s;
AssingServicesToTable(s2);
}
private static void AssingServicesToTable(IEnumerable<ServiceController> serviceControllers)
{
foreach (ServiceController service in serviceControllers)
{
var dr = _dtServices.NewRow();
dr[0] = service.DisplayName;
dr[1] = service.Status;
_dtServices.Rows.Add(dr);
}
}
public static void StartJobAndInvoiceIdMergerService()
{
using (var c = new ServiceController("LiveUploaderJobAndInvoiceIdMergerService"))
{
c.Start();
c.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(10));
}
}
public static void StopJobAndInvoiceIdMergerService()
{
using (var c = new ServiceController("LiveUploaderJobAndInvoiceIdMergerService"))
{
c.Stop();
c.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(10));
}
}
public static void StartUploaderService()
{
using (var c = new ServiceController("LiveUploaderService"))
{
c.Start();
c.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(10));
}
}
public static void StopUploaderService()
{
using (var c = new ServiceController("LiveUploaderService"))
{
c.Stop();
c.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(10));
}
}
}
}
@echo off
"C:\Program Files (x86)\Windows Resource Kits\Tools\subinacl.exe" /service LiveUploaderService /grant=IIS_IUSRS=TO
"C:\Program Files (x86)\Windows Resource Kits\Tools\subinacl.exe" /service LiveUploaderJobAndInvoiceIdMergerService /grant=IIS_IUSRS=TO
pause
Assuming your services are already installed...
First you will need SubInACL (SubInACL.exe)
SubInACL is a command-line tool that enables administrators to obtain security information about files, registry keys, and services, and transfer this information from user to user, from local or global group to group, and from domain to domain.
http://www.microsoft.com/en-us/download/details.aspx?id=23510
Second you will need to run a batch or commands manualy to give Start/Stop rights ("T and O").
Third. Publish your app and test it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment