473,378 Members | 1,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Service Wont Start Programatically (ACCESS DENIED)


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
4 21652
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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: adrian GREEMAN | last post by:
I am fairly much a beginner and have probably done something wrong but version 4.0.12 will not let me in. I have been using MySQL (via MySQL Front and PHPMyAdmin as interfaces usually) on a...
9
by: | last post by:
Hi All, I have allready tried to ask a similar question , but got no answer until now. In the meantime, I found, that I cannot understand some thread-settings for the Main() function . If I use...
1
by: Jason Gleason | last post by:
I am using the following method in a web service that utilizes the system.directoryservices namespace: public ArrayList GetAllAppPools(){ System.DirectoryServices.DirectoryEntry apppools = new...
0
by: Ron Weldy | last post by:
I've looked at just about everything at this point, and I'm still getting the "Error while trying to run project: Unable to start debugging on the web server. Access is denied." message when trying...
2
by: ad | last post by:
I have a virtual which is a web service. When I use the IP to get the web service, it return a access denied message: http://xxx.xxx.xxx.xxx/HealthService/Service.asmx but if I use local host,...
0
by: Telos | last post by:
I'm trying to write a windows service which reads some emails from Exchange Server through WebDAV, using C#.NET 2.0. Everything works fine when testing, using a little Forms application to test...
7
by: =?Utf-8?B?ams=?= | last post by:
I am using System.Diagnostics.Process class to open a word document by call ing Process.Start("test.doc"). I am using C# as programming language. On some of the computers on running this code i get...
1
by: Rick | last post by:
I've migrated a web site from VS 2003 to VS 2005. After pushing the a Windows Server 2003 web server. The Framework 2.0 web site works when I go to test it. I'll start working on something else...
5
by: DotNetDanny | last post by:
Hello Machine: Windows Vista Business, standalone machine (no domain). Installed an old classic ASP webapplication in IIS7, running under a new app.pool with 'NETWORK SERVICE' account (using...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.