I created a asp.net web app as a config tool for a local service. I need to start/restart or stop this service from the web app. i build the next code and the web app is running in asp debug envirment of VS2010. But when i push the start button i get Cannot
open ZRGSService service on computer '.'.
while
(sc.Status == ServiceControllerStatus.Stopped)
{
Thread.Sleep(1000);
sc.Refresh();
}
}
System.InvalidOperationException was unhandled by user code Message=Cannot open ZRGSService service on computer '.'. Source=System.ServiceProcess StackTrace: at System.ServiceProcess.ServiceController.GetServiceHandle(Int32 desiredAccess)
at System.ServiceProcess.ServiceController.Start(String[] args) at System.ServiceProcess.ServiceController.Start() at ZRGSWebConfig.Reboot.startbutton_Click(Object sender, EventArgs e) in C:\Users\W7x64\Documents\Visual Studio 2010\Projects\ZRGSWebConfig\ZRGSWebConfig\Customer
pages\Reboot.aspx.cs:line 108 at System.Web.UI.WebControls.Button.OnClick(EventArgs e) at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) InnerException: System.ComponentModel.Win32Exception Message=Toegang geweigerd ErrorCode=-2147467259 NativeErrorCode=5 InnerException:
My servicecontroller reads all the correct status info from the service.
Your application requires elevated user rights. That is why the "run as administrator" works outside of the IDE.
Some sites suggest disabling UAC. As this is not a possibility in my environment, I decided to request elevated user right via code. Wikipedia provided the help I needed. The
"Requesting elevation" section provides the solution.
The solution in short:
Edit your application manifest file to reflect your requirement.
1.1. Right click your project
1.2. Click "Properties"
1.3. Select "Application" tab - default page
1.4. Click "View UAC Settings" - This button opens the application manifest file (app.manifest)
1.5. Look for the "UAC Manifest Options" section
1.6. Remove or comment the current entry - {requestedExecutionLevel level="asInvoker" uiAccess="false"}
1.7. Change to {requestedExecutionLevel level="requireAdministrator" uiAccess="false"}. MS provides your 3 options as part of the section comment.
EdSteBo
Member
2 Points
2 Posts
Starting and stopping a local windows service from web app.
Feb 29, 2012 01:01 PM|LINK
Hi,
I created a asp.net web app as a config tool for a local service. I need to start/restart or stop this service from the web app. i build the next code and the web app is running in asp debug envirment of VS2010. But when i push the start button i get Cannot open ZRGSService service on computer '.'.
try
{
ServiceController[] scServices;
scServices =ServiceController.GetServices();
foreach (ServiceController scTemp in scServices)
{
if (scTemp.ServiceName == "ZRGSService")
{
Cell1.Text = (sc.Status.ToString());
Cell2.Text = (sc.CanPauseAndContinue.ToString());
Cell3.Text = (sc.CanShutdown.ToString());
Cell4.Text = (sc.CanStop.ToString());
Cell5.Text = (System.Security.Principal.WindowsIdentity.GetCurrent().Name);
and
protected void startbutton_Click(object sender, EventArgs e)
{
if (sc.Status == ServiceControllerStatus.Stopped)
{
sc.Start();
while (sc.Status == ServiceControllerStatus.Stopped)
{
Thread.Sleep(1000);
sc.Refresh();
}
}
System.InvalidOperationException was unhandled by user code Message=Cannot open ZRGSService service on computer '.'. Source=System.ServiceProcess StackTrace: at System.ServiceProcess.ServiceController.GetServiceHandle(Int32 desiredAccess) at System.ServiceProcess.ServiceController.Start(String[] args) at System.ServiceProcess.ServiceController.Start() at ZRGSWebConfig.Reboot.startbutton_Click(Object sender, EventArgs e) in C:\Users\W7x64\Documents\Visual Studio 2010\Projects\ZRGSWebConfig\ZRGSWebConfig\Customer pages\Reboot.aspx.cs:line 108 at System.Web.UI.WebControls.Button.OnClick(EventArgs e) at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) InnerException: System.ComponentModel.Win32Exception Message=Toegang geweigerd ErrorCode=-2147467259 NativeErrorCode=5 InnerException:
My servicecontroller reads all the correct status info from the service.
What is going wrong here? and do i solve this.
Gr.
Edward
niksv
Contributor
5925 Points
1115 Posts
Re: Starting and stopping a local windows service from web app.
Feb 29, 2012 02:00 PM|LINK
http://forums.asp.net/t/1694915.aspx/1
Dino He - MS...
Star
8068 Points
1023 Posts
Microsoft
Re: Starting and stopping a local windows service from web app.
Mar 02, 2012 06:39 AM|LINK
Hi
Please refer to this link:
http://www.csharp-examples.net/restart-windows-service/
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework
EdSteBo
Member
2 Points
2 Posts
Re: Starting and stopping a local windows service from web app.
Mar 02, 2012 07:14 AM|LINK
Sorry Guys,
No go on this, i read the manual and tryed everything but no go.
I think it have something to do with the Windows 7 policy's.
Somebody any idea???
If there is a explenation why things work with XP but not with 7 please inform me.
Greets,
Edward
Dino He - MS...
Star
8068 Points
1023 Posts
Microsoft
Re: Starting and stopping a local windows service from web app.
Mar 05, 2012 02:00 AM|LINK
Hi
I think it's UAC problem:
Your application requires elevated user rights. That is why the "run as administrator" works outside of the IDE.
Some sites suggest disabling UAC. As this is not a possibility in my environment, I decided to request elevated user right via code. Wikipedia provided the help I needed. The "Requesting elevation" section provides the solution.
The solution in short:
Edit your application manifest file to reflect your requirement.
1.1. Right click your project
1.2. Click "Properties"
1.3. Select "Application" tab - default page
1.4. Click "View UAC Settings" - This button opens the application manifest file (app.manifest)
1.5. Look for the "UAC Manifest Options" section
1.6. Remove or comment the current entry - {requestedExecutionLevel level="asInvoker" uiAccess="false"}
1.7. Change to {requestedExecutionLevel level="requireAdministrator" uiAccess="false"}. MS provides your 3 options as part of the section comment.
And you can see this:
http://support.microsoft.com/kb/306158
Hope it helpful.
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework