By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,905 Members | 902 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,905 IT Pros & Developers. It's quick & easy.

Service Wont Start Programatically (ACCESS DENIED)

P: n/a

I have written two windows services:
- service A does some crunching of local data files and uploads them to
a central processing computer via http.
- service B monitors a manifest file on a webserver to see if service A
needs to be updated.

What service B does if it sees their is an update for service A is to
download a new copy of the service A executable, stop service A,
replace the executable with the new copy, and start service B back up.
Everything works fine until that very last step. I can programatically
stop and start the service just fine, if the service A executable was
placed their manually. If service B downloads and replaces the service
A executable, it can't then restart service A. When starting service A
it dies with

System.InvalidOperationException: Cannot start service Service A on
computer '.'. ---System.ComponentModel.Win32Exception: Access is
denied
--- End of inner exception stack trace ---
at System.ServiceProcess.ServiceController.Start(Stri ng[] args)
at System.ServiceProcess.ServiceController.Start()
at namespace.ServiceB.t_update_Elapsed(Object sender,
ElapsedEventArgs e)

I am assuming this is just a permissions issue somwhere. Service A and
Service B are both installed with ServiceProcessInstaller.Account =
ServiceAccount.LocalSystem and Username and Password = null;

Any thoughts? Thanks in advance for your help, i'm a general newb at
windows application development.

Jan 14 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
If service B does nothing more than what you have said then I would suggest
that having that functionality as a seperate service is overkill.

A solid technique is to have service A monitor the need for an upgrade aand
download the upgraded executable as a temporary file. When it has completed
this step, have it use Process.Start to spawn a 'stub' program.

The stub program stops service A, replaces the executable with the
downloaded (upgraded) version and starts service A. Because the 'stub'
program is spawned by service A, it runs under thae same account
(LocalSystem) and so, for the short period of time involved, it needs to
impersonate an account with the necessary permissions.

This means that the 'stub' program only runs when it needs to and you don't
have the overhead of running a extra service.

The logic for the 'stub' program is relatively simple:

static void Main()
{

// Start impersonating a priviledged account

// Stop Service A

// Replace the executable for Service A

// Start Service A

// Cease impersonation

}
<ca****@carsonevans.comwrote in message
news:11*********************@l53g2000cwa.googlegro ups.com...
>
I have written two windows services:
- service A does some crunching of local data files and uploads them to
a central processing computer via http.
- service B monitors a manifest file on a webserver to see if service A
needs to be updated.

What service B does if it sees their is an update for service A is to
download a new copy of the service A executable, stop service A,
replace the executable with the new copy, and start service B back up.
Everything works fine until that very last step. I can programatically
stop and start the service just fine, if the service A executable was
placed their manually. If service B downloads and replaces the service
A executable, it can't then restart service A. When starting service A
it dies with

System.InvalidOperationException: Cannot start service Service A on
computer '.'. ---System.ComponentModel.Win32Exception: Access is
denied
--- End of inner exception stack trace ---
at System.ServiceProcess.ServiceController.Start(Stri ng[] args)
at System.ServiceProcess.ServiceController.Start()
at namespace.ServiceB.t_update_Elapsed(Object sender,
ElapsedEventArgs e)

I am assuming this is just a permissions issue somwhere. Service A and
Service B are both installed with ServiceProcessInstaller.Account =
ServiceAccount.LocalSystem and Username and Password = null;

Any thoughts? Thanks in advance for your help, i'm a general newb at
windows application development.

Jan 14 '07 #2

P: n/a
I have implemented Stephany's suggestion, at least I think I have. :)

I now have a single process, Service A, and a "controller" executeable.
The controller code looks like this...

if (File.Exists(s_downloadFile))
{
alterProcessStatus("stop");
File.Replace(s_downloadFile, s_installFile,
s_backupFile);
alterProcessStatus("start");
}

Where alterProcessStatus does...

ServiceController sc = new ServiceController("Service A");
sc.Refresh();
/** all my logic to handle stopping/starting the service
* depending on the process current state along with some
* sc.WaitForStatus() just to make sure it works.
**/

Service A now handles downloading new copies of "itself" when updates
are ready. When an update is successfully downloaded I kick off a
System.Diagnostic.Process.Start(s_controllerPath); and it does manage
to stop the service and replace the ServiceA.exe with the downloaded
version, however when starting the function I still get..

System.InvalidOperationException: Cannot start service Service A on
computer '.'. ---System.ComponentModel.Win32Exception: Access is
denied
--- End of inner exception stack trace ---
at System.ServiceProcess.ServiceController.Start(Stri ng[] args)
at System.ServiceProcess.ServiceController.Start()
at Controller.Program.alterProcessStatus(String arg)
at Controller.Program.Main(String[] args)

Trying to use the Start in the Service Manager gives me the same Access
Denied, the event viewer said "The Service A service failed to start
due to the following error: Access is denied."

I created a rollback function in Controller and it is able to copy the
files back to how they were before the update and restart the process.
This got me thinking, it's obviously not the controllers fault since it
is able to manipulate the service and overwrite the files just fine. If
I manually put my downloaded file in the download location (instead of
letting the service download it) and then let the service run (it
doesn't download a new file if there is already a patch file waiting)
it is able to spawn the process to update itself just fine.

It would seem this is more about how the file is being delivered, not
how the update happens. So, maybe the way I go about downloading a new
file is the issue...

HttpWebRequest webrequest =
(HttpWebRequest)WebRequest.Create(uri_newFile);
WebResponse wr_response = webrequest.GetResponse();
StreamReader sr_response = new
StreamReader(wr_response.GetResponseStream());
StreamWriter sw_out = new StreamWriter(new FileStream(s_downloadFile,
FileMode.Create));
sw_out.Write(sr_response.ReadToEnd());
wr_response.Close();
sr_response.Close();
sw_out.Close();

There is no difference byte wise between this code downloading a file
from my webserver and me placing the file directly in the
s_downloadFile location. This still leads me to believe it's all about
the permissions on the downloaded file. Anyone have any clues? What
tools are there to compare the permissions between two files on XP?

Stephany Young wrote:
If service B does nothing more than what you have said then I would suggest
that having that functionality as a seperate service is overkill.

A solid technique is to have service A monitor the need for an upgrade aand
download the upgraded executable as a temporary file. When it has completed
this step, have it use Process.Start to spawn a 'stub' program.

The stub program stops service A, replaces the executable with the
downloaded (upgraded) version and starts service A. Because the 'stub'
program is spawned by service A, it runs under thae same account
(LocalSystem) and so, for the short period of time involved, it needs to
impersonate an account with the necessary permissions.

This means that the 'stub' program only runs when it needs to and you don't
have the overhead of running a extra service.

The logic for the 'stub' program is relatively simple:

static void Main()
{

// Start impersonating a priviledged account

// Stop Service A

// Replace the executable for Service A

// Start Service A

// Cease impersonation

}
<ca****@carsonevans.comwrote in message
news:11*********************@l53g2000cwa.googlegro ups.com...

I have written two windows services:
- service A does some crunching of local data files and uploads them to
a central processing computer via http.
- service B monitors a manifest file on a webserver to see if service A
needs to be updated.

What service B does if it sees their is an update for service A is to
download a new copy of the service A executable, stop service A,
replace the executable with the new copy, and start service B back up.
Everything works fine until that very last step. I can programatically
stop and start the service just fine, if the service A executable was
placed their manually. If service B downloads and replaces the service
A executable, it can't then restart service A. When starting service A
it dies with

System.InvalidOperationException: Cannot start service Service A on
computer '.'. ---System.ComponentModel.Win32Exception: Access is
denied
--- End of inner exception stack trace ---
at System.ServiceProcess.ServiceController.Start(Stri ng[] args)
at System.ServiceProcess.ServiceController.Start()
at namespace.ServiceB.t_update_Elapsed(Object sender,
ElapsedEventArgs e)

I am assuming this is just a permissions issue somwhere. Service A and
Service B are both installed with ServiceProcessInstaller.Account =
ServiceAccount.LocalSystem and Username and Password = null;

Any thoughts? Thanks in advance for your help, i'm a general newb at
windows application development.
Jan 14 '07 #3

P: n/a
Yes. You could very well be right.

It might be possible that your download 'mechanism' is not 'releasing' the
file correctly and that could be causing the exception.

Although the StreamWriter.Close method also (is supposed to) closes the
underlying stream, I have heard tell of situations where files are not
'released properly. (I haven't experienced it myself so I don't know what
the circumstances are.)

A couple of things to try:

Change the statement:

StreamWriter sw_out = new StreamWriter(new FileStream(s_downloadFile,
FileMode.Create));

to:

FileStream fs = new FileStream(s_downloadFile, FileMode.Create);
StreamWriter sw_out = new StreamWriter(fs);

and add:

fs.Close();

after:

sw_out.Close();

You might also need to add one or both of:

sw_out.Dispose();
fs.Dispose();

or:

You could try using the WebClient class to do the download, something
like:

WebClient webclient = new WebClient();

// Download the Web resource and save it using the filesystem filename.
webclient.DownloadFile(uri_newFile, s_downloadFile);

webclient.Dispose();

// Note the explicit dispose

or:

using (WebClient webclient = new WebClient())
{
// Download the Web resource and save it using the filesystem
filename.
webclient.DownloadFile(uri_newFile, s_downloadFile);
}

The 'using' keyword automatically disposes of the 'using' object
(webclient) at the end of the block.
<ca****@carsonevans.comwrote in message
news:11**********************@51g2000cwl.googlegro ups.com...
>I have implemented Stephany's suggestion, at least I think I have. :)

I now have a single process, Service A, and a "controller" executeable.
The controller code looks like this...

if (File.Exists(s_downloadFile))
{
alterProcessStatus("stop");
File.Replace(s_downloadFile, s_installFile,
s_backupFile);
alterProcessStatus("start");
}

Where alterProcessStatus does...

ServiceController sc = new ServiceController("Service A");
sc.Refresh();
/** all my logic to handle stopping/starting the service
* depending on the process current state along with some
* sc.WaitForStatus() just to make sure it works.
**/

Service A now handles downloading new copies of "itself" when updates
are ready. When an update is successfully downloaded I kick off a
System.Diagnostic.Process.Start(s_controllerPath); and it does manage
to stop the service and replace the ServiceA.exe with the downloaded
version, however when starting the function I still get..

System.InvalidOperationException: Cannot start service Service A on
computer '.'. ---System.ComponentModel.Win32Exception: Access is
denied
--- End of inner exception stack trace ---
at System.ServiceProcess.ServiceController.Start(Stri ng[] args)
at System.ServiceProcess.ServiceController.Start()
at Controller.Program.alterProcessStatus(String arg)
at Controller.Program.Main(String[] args)

Trying to use the Start in the Service Manager gives me the same Access
Denied, the event viewer said "The Service A service failed to start
due to the following error: Access is denied."

I created a rollback function in Controller and it is able to copy the
files back to how they were before the update and restart the process.
This got me thinking, it's obviously not the controllers fault since it
is able to manipulate the service and overwrite the files just fine. If
I manually put my downloaded file in the download location (instead of
letting the service download it) and then let the service run (it
doesn't download a new file if there is already a patch file waiting)
it is able to spawn the process to update itself just fine.

It would seem this is more about how the file is being delivered, not
how the update happens. So, maybe the way I go about downloading a new
file is the issue...

HttpWebRequest webrequest =
(HttpWebRequest)WebRequest.Create(uri_newFile);
WebResponse wr_response = webrequest.GetResponse();
StreamReader sr_response = new
StreamReader(wr_response.GetResponseStream());
StreamWriter sw_out = new StreamWriter(new FileStream(s_downloadFile,
FileMode.Create));
sw_out.Write(sr_response.ReadToEnd());
wr_response.Close();
sr_response.Close();
sw_out.Close();

There is no difference byte wise between this code downloading a file
from my webserver and me placing the file directly in the
s_downloadFile location. This still leads me to believe it's all about
the permissions on the downloaded file. Anyone have any clues? What
tools are there to compare the permissions between two files on XP?

Stephany Young wrote:
>If service B does nothing more than what you have said then I would
suggest
that having that functionality as a seperate service is overkill.

A solid technique is to have service A monitor the need for an upgrade
aand
download the upgraded executable as a temporary file. When it has
completed
this step, have it use Process.Start to spawn a 'stub' program.

The stub program stops service A, replaces the executable with the
downloaded (upgraded) version and starts service A. Because the 'stub'
program is spawned by service A, it runs under thae same account
(LocalSystem) and so, for the short period of time involved, it needs to
impersonate an account with the necessary permissions.

This means that the 'stub' program only runs when it needs to and you
don't
have the overhead of running a extra service.

The logic for the 'stub' program is relatively simple:

static void Main()
{

// Start impersonating a priviledged account

// Stop Service A

// Replace the executable for Service A

// Start Service A

// Cease impersonation

}
<ca****@carsonevans.comwrote in message
news:11*********************@l53g2000cwa.googlegr oups.com...
>
I have written two windows services:
- service A does some crunching of local data files and uploads them to
a central processing computer via http.
- service B monitors a manifest file on a webserver to see if service A
needs to be updated.

What service B does if it sees their is an update for service A is to
download a new copy of the service A executable, stop service A,
replace the executable with the new copy, and start service B back up.
Everything works fine until that very last step. I can programatically
stop and start the service just fine, if the service A executable was
placed their manually. If service B downloads and replaces the service
A executable, it can't then restart service A. When starting service A
it dies with

System.InvalidOperationException: Cannot start service Service A on
computer '.'. ---System.ComponentModel.Win32Exception: Access is
denied
--- End of inner exception stack trace ---
at System.ServiceProcess.ServiceController.Start(Stri ng[] args)
at System.ServiceProcess.ServiceController.Start()
at namespace.ServiceB.t_update_Elapsed(Object sender,
ElapsedEventArgs e)

I am assuming this is just a permissions issue somwhere. Service A and
Service B are both installed with ServiceProcessInstaller.Account =
ServiceAccount.LocalSystem and Username and Password = null;

Any thoughts? Thanks in advance for your help, i'm a general newb at
windows application development.

Jan 14 '07 #4

P: n/a

Thank you very much, Stephany.

Swtiching my code over the WebClient.DownloadFile approach made it all
work. I still don't see what in my code was any different than what
WebClient.DownloadFile does, but hey, it works and my code is cleaner
to boot.

Thanks again!
Stephany Young wrote:
Yes. You could very well be right.

It might be possible that your download 'mechanism' is not 'releasing' the
file correctly and that could be causing the exception.

Although the StreamWriter.Close method also (is supposed to) closes the
underlying stream, I have heard tell of situations where files are not
'released properly. (I haven't experienced it myself so I don't know what
the circumstances are.)

A couple of things to try:

Change the statement:

StreamWriter sw_out = new StreamWriter(new FileStream(s_downloadFile,
FileMode.Create));

to:

FileStream fs = new FileStream(s_downloadFile, FileMode.Create);
StreamWriter sw_out = new StreamWriter(fs);

and add:

fs.Close();

after:

sw_out.Close();

You might also need to add one or both of:

sw_out.Dispose();
fs.Dispose();

or:

You could try using the WebClient class to do the download, something
like:

WebClient webclient = new WebClient();

// Download the Web resource and save it using the filesystem filename.
webclient.DownloadFile(uri_newFile, s_downloadFile);

webclient.Dispose();

// Note the explicit dispose

or:

using (WebClient webclient = new WebClient())
{
// Download the Web resource and save it using the filesystem
filename.
webclient.DownloadFile(uri_newFile, s_downloadFile);
}

The 'using' keyword automatically disposes of the 'using' object
(webclient) at the end of the block.
<ca****@carsonevans.comwrote in message
news:11**********************@51g2000cwl.googlegro ups.com...
I have implemented Stephany's suggestion, at least I think I have. :)

I now have a single process, Service A, and a "controller" executeable.
The controller code looks like this...

if (File.Exists(s_downloadFile))
{
alterProcessStatus("stop");
File.Replace(s_downloadFile, s_installFile,
s_backupFile);
alterProcessStatus("start");
}

Where alterProcessStatus does...

ServiceController sc = new ServiceController("Service A");
sc.Refresh();
/** all my logic to handle stopping/starting the service
* depending on the process current state along with some
* sc.WaitForStatus() just to make sure it works.
**/

Service A now handles downloading new copies of "itself" when updates
are ready. When an update is successfully downloaded I kick off a
System.Diagnostic.Process.Start(s_controllerPath); and it does manage
to stop the service and replace the ServiceA.exe with the downloaded
version, however when starting the function I still get..

System.InvalidOperationException: Cannot start service Service A on
computer '.'. ---System.ComponentModel.Win32Exception: Access is
denied
--- End of inner exception stack trace ---
at System.ServiceProcess.ServiceController.Start(Stri ng[] args)
at System.ServiceProcess.ServiceController.Start()
at Controller.Program.alterProcessStatus(String arg)
at Controller.Program.Main(String[] args)

Trying to use the Start in the Service Manager gives me the same Access
Denied, the event viewer said "The Service A service failed to start
due to the following error: Access is denied."

I created a rollback function in Controller and it is able to copy the
files back to how they were before the update and restart the process.
This got me thinking, it's obviously not the controllers fault since it
is able to manipulate the service and overwrite the files just fine. If
I manually put my downloaded file in the download location (instead of
letting the service download it) and then let the service run (it
doesn't download a new file if there is already a patch file waiting)
it is able to spawn the process to update itself just fine.

It would seem this is more about how the file is being delivered, not
how the update happens. So, maybe the way I go about downloading a new
file is the issue...

HttpWebRequest webrequest =
(HttpWebRequest)WebRequest.Create(uri_newFile);
WebResponse wr_response = webrequest.GetResponse();
StreamReader sr_response = new
StreamReader(wr_response.GetResponseStream());
StreamWriter sw_out = new StreamWriter(new FileStream(s_downloadFile,
FileMode.Create));
sw_out.Write(sr_response.ReadToEnd());
wr_response.Close();
sr_response.Close();
sw_out.Close();

There is no difference byte wise between this code downloading a file
from my webserver and me placing the file directly in the
s_downloadFile location. This still leads me to believe it's all about
the permissions on the downloaded file. Anyone have any clues? What
tools are there to compare the permissions between two files on XP?

Stephany Young wrote:
If service B does nothing more than what you have said then I would
suggest
that having that functionality as a seperate service is overkill.

A solid technique is to have service A monitor the need for an upgrade
aand
download the upgraded executable as a temporary file. When it has
completed
this step, have it use Process.Start to spawn a 'stub' program.

The stub program stops service A, replaces the executable with the
downloaded (upgraded) version and starts service A. Because the 'stub'
program is spawned by service A, it runs under thae same account
(LocalSystem) and so, for the short period of time involved, it needs to
impersonate an account with the necessary permissions.

This means that the 'stub' program only runs when it needs to and you
don't
have the overhead of running a extra service.

The logic for the 'stub' program is relatively simple:

static void Main()
{

// Start impersonating a priviledged account

// Stop Service A

// Replace the executable for Service A

// Start Service A

// Cease impersonation

}
<ca****@carsonevans.comwrote in message
news:11*********************@l53g2000cwa.googlegro ups.com...

I have written two windows services:
- service A does some crunching of local data files and uploads them to
a central processing computer via http.
- service B monitors a manifest file on a webserver to see if service A
needs to be updated.

What service B does if it sees their is an update for service A is to
download a new copy of the service A executable, stop service A,
replace the executable with the new copy, and start service B back up.
Everything works fine until that very last step. I can programatically
stop and start the service just fine, if the service A executable was
placed their manually. If service B downloads and replaces the service
A executable, it can't then restart service A. When starting service A
it dies with

System.InvalidOperationException: Cannot start service Service A on
computer '.'. ---System.ComponentModel.Win32Exception: Access is
denied
--- End of inner exception stack trace ---
at System.ServiceProcess.ServiceController.Start(Stri ng[] args)
at System.ServiceProcess.ServiceController.Start()
at namespace.ServiceB.t_update_Elapsed(Object sender,
ElapsedEventArgs e)

I am assuming this is just a permissions issue somwhere. Service A and
Service B are both installed with ServiceProcessInstaller.Account =
ServiceAccount.LocalSystem and Username and Password = null;

Any thoughts? Thanks in advance for your help, i'm a general newb at
windows application development.
Jan 15 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.