I have the below methods to start and stop a service. I call this method from another Console application to debug since I've used the methods in a Class Library(DLL).
The application is started with administrative rights.
public void ServiceStart()
{
ServiceController service = new ServiceController();
service.ServiceName = "ASP.NET State Service";
service.Start();
}
public void ServiceStop()
{
ServiceController service = new ServiceController();
service.ServiceName = "ASP.NET State Service";
service.Stop();
}
But when I call Start() or Stop() an exception with the following message is thrown:
Cannot open ASP.NET State Service service on computer '.'
Can someone help me out?
You have to pass the Service name, not the Display name.
Always check the service properties in the "Services" application.
Try again with
service.ServiceName = "aspnet_state";
Alternatively, you can create the ServiceController instance using the display name:
ServiceController service = new ServiceController("ASP.NET State Service");
since the documentation for the constructor argument says:
The name that identifies the service to the system. This can also be
the display name for the service.
Also note that a call to
service.Start();
returns immediately, without waiting for the service to start.
You should call
service.WaitForStatus(ServiceControllerStatus.Running);
if you want to make sure the service is running before your application continues.
Open Visual studio as Administrator
Related
I have created a windows service which I would like to run whenever Windows starts. To run it after boot completion only, I have inserted below code in installer of service:
this.WinSvcInstaller.DelayedAutoStart = true;
The service is made to run immediately after installation using below code:
private void WinSvcInstaller_AfterInstall(object sender, InstallEventArgs e)
{
ServiceController sc = new ServiceController(ServiceName);
sc.Start();
}
where ServiceName is property created in WinSvcInstaller.
Once the service completed whether successfully or unsuccessfully it made to stop using below code automatically:
ProjectInstaller projectInstaller = new ProjectInstaller();
ServiceController serviceController = new ServiceController(projectInstaller.ServiceName);
if (serviceController.Status.Equals(ServiceControllerStatus.Running)
|| serviceController.Status.Equals(ServiceControllerStatus.StartPending))
{
this.Stop();
}
My main problem is that the service doesn't run unless I reboot computer meaning, the service runs only if I restart the computer. If I do shut down and then start computer manually again after the shut down then the service doesn't run. Can anybody please help me why is this happening??? What I am missing?
I have a windows service which should stop after some particular date time.
I am able to stop the service programmatically using:
try
{
ServiceController service = new ServiceController(servicename);
//or
//ServiceController service = new ServiceController(servicename,computer name);
service.Stop();
}
catch(Exception ex)
{
}
finally
{
ServiceController service = new ServiceController(servicename);
service.Stop();
}
But it throws an exception and does not stop if we remove the finally part.
It stopped only after I used the finally statement but obviously the exception persists.
Exception message:
Cannot open "Service Name" service on computer '.'
detailed exception:
Cannot open Service Control Manager on computer 'computer name'. This operation might require other privileges.
I did refer but it does not help also it is not addresses my problem:
Stackoverflow Question
How can I stop the service programmatically without this exception
Right click on the program and choose "Run As Administrator". If you are debugging it, ensure the IDE ( ie Visual Studio I suppose ) is opened with "Run as Administrator".
When I had this problem I went to the machine in question, and tried to stop the service manually though the interface, and received this pop-up:
I was able to stop the service by finding the task in Task Manager and ending that task. Not sure what exactly hung up the service but this works if you have access to the machine and task manager.
I was able to start the service and use the ServiceController code without any issues after.
I work on windows service project , i want to restart my windows service when Lync Front-End service was restarted
i know how i restart my windows service i use this answer , but i don't know how i restart it when the Front-End Service was restarted
and i know i can check the windows service status by use ServiceController Class
Run this code in your service :
EventLog log = EventLog.GetEventLogs().First(o => o.Log == "Security");
log.EnableRaisingEvents = true;
log.EntryWritten += (s, e) => {p.print(e); };
Write a method for event log
void test(EntryWrittenEventArgs entry)
{
//you can check event log in the log viewer and set
//EventLogEntryType and InstanceId accordingly.
EventLogEntry evntLog=entry.Entry;
if (evntLog.EntryType == EventLogEntryType.SuccessAudit &&
evntLog.InstanceId==123)
{
//Code to restart the service
}
}
I) Topshelf is an open source project for developing Windows Services easily. It will save you time and even if you decide not to use it, you can learn from it's source code.
II) The short answer is you have to poll the status of the other service. There is no (plain) event based mechanism for this.
If you can not make any modifications to the other service then you can poll over it's status by (in a separate Task):
var sc = new ServiceController(serviceName);
status = sc.Status;
And use the answer you've mentioned to restart yours (and again poll in OnStart until the other service get started completely).
But if you can modify the other service then you can use other means like pipes or named mutexes to do that (again needs polling in a separate Task or at OnStart and probably at OnStop).
Normally when it is required to know the one of the service status in the existing server means it is easy by from finding the result from the servicecontroller by pasted below code:
ServiceController sc = new ServiceController("servicename");
if ((sc.Status.Equals(ServiceControllerStatus.Stopped)) ||
(sc.Status.Equals(ServiceControllerStatus.StopPending)))
{
// Start the service if the current status is stopped.
sc.Start();
}
Here i would like to know the service status of another machine, how can i find out the result can any one able to guide me on this scenario.
There's another constructor of ServiceController that takes the machine name as a second parameter. http://msdn.microsoft.com/en-us/library/ssbk2tf3.aspx
Have you tried it?
I'm using the ServiceController class to start a (custom) installed service, like this:
var newServiceController = new ServiceController("theNameOfMyService");
newServiceController.Start();
Trouble is, the service always runs under the local system account, and instead I want it to run under my account.
Can anyone tell me how to use ServiceController to run a service under a different account?
I don't think you can... the credentials for the service are provided at install time or alternatively via the Service.msc snap in
The service controller can only start/stop a pre-installed service
On the ServiceProcessInstaller instance for you service there is a property called Acount, Password and Username, can use these properties to set who the service runs as.