Starting and stopping a local windows service from web app.http://forums.asp.net/t/1775128.aspx/1?Starting+and+stopping+a+local+windows+service+from+web+app+Mon, 05 Mar 2012 02:00:39 -050017751284856890http://forums.asp.net/p/1775128/4856890.aspx/1?Starting+and+stopping+a+local+windows+service+from+web+app+Starting and stopping a local windows service from web app. <p>Hi,</p> <p>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 '.'.</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p> <p></p> <p><span face="Consolas" size="2" color="#0000ff" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#0000ff" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#0000ff" style="color:#0000ff; font-family:Consolas; font-size:small">try</span></span></span></p> <p>{</p> <p>&nbsp;<span face="Consolas" size="2" color="#2b91af" style="color:#2b91af; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#2b91af" style="color:#2b91af; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#2b91af" style="color:#2b91af; font-family:Consolas; font-size:small">ServiceController</span></span></span><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"><span face="Consolas" size="2" style="font-family:Consolas; font-size:small">[] scServices;</span></span></p> <p>scServices =<span face="Consolas" size="2" color="#2b91af" style="color:#2b91af; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#2b91af" style="color:#2b91af; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#2b91af" style="color:#2b91af; font-family:Consolas; font-size:small">ServiceController</span></span></span><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"><span face="Consolas" size="2" style="font-family:Consolas; font-size:small">.GetServices();</span></span></p> <p><span face="Consolas" size="2" color="#0000ff" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#0000ff" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#0000ff" style="color:#0000ff; font-family:Consolas; font-size:small">foreach</span></span></span><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"> (</span></span><span face="Consolas" size="2" color="#2b91af" style="color:#2b91af; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#2b91af" style="color:#2b91af; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#2b91af" style="color:#2b91af; font-family:Consolas; font-size:small">ServiceController</span></span></span><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"> scTemp </span></span><span face="Consolas" size="2" color="#0000ff" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#0000ff" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#0000ff" style="color:#0000ff; font-family:Consolas; font-size:small">in</span></span></span><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"> scServices)</span></span></p> <p>{</p> <p><span face="Consolas" size="2" color="#0000ff" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#0000ff" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#0000ff" style="color:#0000ff; font-family:Consolas; font-size:small">if</span></span></span><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"> (scTemp.ServiceName == </span></span><span face="Consolas" size="2" color="#a31515" style="color:#a31515; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#a31515" style="color:#a31515; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#a31515" style="color:#a31515; font-family:Consolas; font-size:small">&quot;ZRGSService&quot;</span></span></span><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"><span face="Consolas" size="2" style="font-family:Consolas; font-size:small">)</span></span></p> <p>{</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cell1.Text = (sc.Status.ToString());</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cell2.Text = (sc.CanPauseAndContinue.ToString());</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cell3.Text = (sc.CanShutdown.ToString());</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cell4.Text = (sc.CanStop.ToString());</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cell5.Text = (System.Security.Principal.<span face="Consolas" size="2" color="#2b91af" style="color:#2b91af; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#2b91af" style="color:#2b91af; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#2b91af" style="color:#2b91af; font-family:Consolas; font-size:small">WindowsIdentity</span></span></span><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"><span face="Consolas" size="2" style="font-family:Consolas; font-size:small">.GetCurrent().Name);</span></span></p> <p><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"><span face="Consolas" size="2" style="font-family:Consolas; font-size:small">and</span></span></p> <p><span face="Consolas" size="2" color="#0000ff" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#0000ff" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#0000ff" style="color:#0000ff; font-family:Consolas; font-size:small">protected</span></span></span><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"> </span></span><span face="Consolas" size="2" color="#0000ff" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#0000ff" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#0000ff" style="color:#0000ff; font-family:Consolas; font-size:small">void</span></span></span><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"> startbutton_Click(</span></span><span face="Consolas" size="2" color="#0000ff" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#0000ff" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#0000ff" style="color:#0000ff; font-family:Consolas; font-size:small">object</span></span></span><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"> sender, </span></span><span face="Consolas" size="2" color="#2b91af" style="color:#2b91af; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#2b91af" style="color:#2b91af; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#2b91af" style="color:#2b91af; font-family:Consolas; font-size:small">EventArgs</span></span></span><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"> e)</span></span></p> <p>{</p> <p><span face="Consolas" size="2" color="#0000ff" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#0000ff" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#0000ff" style="color:#0000ff; font-family:Consolas; font-size:small">if</span></span></span><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"> (sc.Status == </span></span><span face="Consolas" size="2" color="#2b91af" style="color:#2b91af; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#2b91af" style="color:#2b91af; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#2b91af" style="color:#2b91af; font-family:Consolas; font-size:small">ServiceControllerStatus</span></span></span><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"><span face="Consolas" size="2" style="font-family:Consolas; font-size:small">.Stopped)</span></span></p> <p>{</p> <p>sc.Start();</p> <p><span face="Consolas" size="2" color="#0000ff" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#0000ff" style="color:#0000ff; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#0000ff" style="color:#0000ff; font-family:Consolas; font-size:small">while</span></span></span><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"> (sc.Status == </span></span><span face="Consolas" size="2" color="#2b91af" style="color:#2b91af; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#2b91af" style="color:#2b91af; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#2b91af" style="color:#2b91af; font-family:Consolas; font-size:small">ServiceControllerStatus</span></span></span><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"><span face="Consolas" size="2" style="font-family:Consolas; font-size:small">.Stopped)</span></span></p> <p>{</p> <p><span face="Consolas" size="2" color="#2b91af" style="color:#2b91af; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#2b91af" style="color:#2b91af; font-family:Consolas; font-size:small"><span face="Consolas" size="2" color="#2b91af" style="color:#2b91af; font-family:Consolas; font-size:small">Thread</span></span></span><span face="Consolas" size="2" style="font-family:Consolas; font-size:small"><span face="Consolas" size="2" style="font-family:Consolas; font-size:small">.Sleep(1000);</span></span></p> <p>sc.Refresh();</p> <p>}</p> <p>}</p> <p>System.InvalidOperationException was unhandled by user code &nbsp; Message=Cannot open ZRGSService service on computer '.'. &nbsp; Source=System.ServiceProcess &nbsp; StackTrace: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.ServiceProcess.ServiceController.GetServiceHandle(Int32 desiredAccess) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.ServiceProcess.ServiceController.Start(String[] args) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.ServiceProcess.ServiceController.Start() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.Web.UI.WebControls.Button.OnClick(EventArgs e) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) &nbsp; InnerException: System.ComponentModel.Win32Exception &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Message=Toegang geweigerd &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ErrorCode=-2147467259 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NativeErrorCode=5 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; InnerException:</p> <p>My servicecontroller&nbsp;reads all the correct status info from the service.</p> <p>What is going wrong here? and do i solve this.</p> <p>Gr.</p> <p>Edward&nbsp;</p> 2012-02-29T13:01:26-05:004856967http://forums.asp.net/p/1775128/4856967.aspx/1?Re+Starting+and+stopping+a+local+windows+service+from+web+app+Re: Starting and stopping a local windows service from web app. <p><a href="http://forums.asp.net/t/1694915.aspx/1">http://forums.asp.net/t/1694915.aspx/1</a></p> 2012-02-29T14:00:37-05:004860170http://forums.asp.net/p/1775128/4860170.aspx/1?Re+Starting+and+stopping+a+local+windows+service+from+web+app+Re: Starting and stopping a local windows service from web app. <p>Hi</p> <p>Please refer to this link:</p> <p><a href="http://www.csharp-examples.net/restart-windows-service/">http://www.csharp-examples.net/restart-windows-service/</a></p> 2012-03-02T06:39:29-05:004860223http://forums.asp.net/p/1775128/4860223.aspx/1?Re+Starting+and+stopping+a+local+windows+service+from+web+app+Re: Starting and stopping a local windows service from web app. <p>Sorry Guys,</p> <p>No go on this, i read the manual and tryed everything but no go.</p> <p>I think it have something to do with the Windows 7 policy's.</p> <p>Somebody any idea???</p> <p>If there is a explenation why things work with XP but not with 7 please inform me.</p> <p>Greets,</p> <p>Edward</p> 2012-03-02T07:14:57-05:004863221http://forums.asp.net/p/1775128/4863221.aspx/1?Re+Starting+and+stopping+a+local+windows+service+from+web+app+Re: Starting and stopping a local windows service from web app. <p>Hi</p> <p>I think it's UAC problem:</p> <p>Your application requires elevated user rights. That is why the &quot;run as administrator&quot; works outside of the IDE.</p> <p>Some sites suggest disabling UAC. As this is not a possibility in my environment, I decided to request elevated user right via code.&nbsp;<a href="http://en.wikipedia.org/wiki/User_Account_Control" rel="nofollow">Wikipedia</a>&nbsp;provided the help I needed. The &quot;Requesting elevation&quot; section provides the solution.</p> <p><strong>The solution in short:</strong></p> <hr> <p>Edit your application manifest file to reflect your requirement.</p> <hr> <p>1.1. Right click your project<br> 1.2. Click &quot;Properties&quot;<br> 1.3. Select &quot;Application&quot; tab - default page<br> 1.4. Click &quot;View UAC Settings&quot; - This button opens the application manifest file (app.manifest)<br> 1.5. Look for the &quot;UAC Manifest Options&quot; section&nbsp;<br> 1.6. Remove or comment the current entry - {requestedExecutionLevel level=&quot;asInvoker&quot; uiAccess=&quot;false&quot;}<br> 1.7. Change to {requestedExecutionLevel level=&quot;requireAdministrator&quot; uiAccess=&quot;false&quot;}. MS provides your 3 options as part of the section comment.</p> <p></p> <p>And you can see this:</p> <p><a href="http://support.microsoft.com/kb/306158">http://support.microsoft.com/kb/306158</a></p> <p>Hope it helpful.</p> 2012-03-05T02:00:39-05:00