Rohit Ramname
Rohit Ramname

Reputation: 834

Can not open service error

I have written a windows service. I am trying to start the service from my web applicaiton.

ServiceController svcContrl = new ServiceController("TestService");
svcContrl.Start();

I can see the service in My Computer->Manage->Service. If I start the service from there manually, it works fine. But when I try to run the code, I am getting an exception " Cannot open TestService service on [ComputerName]".

I have given access to aspnet user, windows login user to service, bin/debug folders still getting this exception when the code runs. Am i missing something which is causing this exception?

Upvotes: 0

Views: 1876

Answers (2)

Rohit Ramname
Rohit Ramname

Reputation: 834

I got the solution. I am using Windows 2008 R2 IIS 6.1. It by default connects as Network service. Hence I needed to change the basic settings of my virtual directory and set it to connect as my machine log in credentials. Thanks All and especially Chris.

Upvotes: 1

ChrisLively
ChrisLively

Reputation: 88064

The InvalidOperationException can be thrown when the service isn't located or if there is a bug within the service itself preventing it from starting.

Given that you said you can start the service directly through the MMC app, it sounds like maybe you have the wrong name for the service in your code.

Try the following: http://msdn.microsoft.com/en-us/library/yb9w7ytd.aspx to see if the service can be started/stopped/etc.

This code (http://msdn.microsoft.com/en-us/library/hyd7kz8c(v=vs.90).aspx) shows how to get a list of services on the system. Might be useful to determine the actual service name.

And, here is a sample app that starts/stops services: http://www.codeproject.com/Articles/31688/Using-the-ServiceController-in-C-to-stop-and-start

Upvotes: 1

Related Questions