Open davisford opened 6 years ago
Here are some thoughts for you. I've never used TopShelf before, so please bear with me.
I believe that "Access Denied" that you are seeing, is an artifact of TopShelf wrongly passing it's internal error code to Windows Service Control Manager. I opened an issue with them.
The actual issue that is happening to you is an exception thrown inside StartProcessAsCurrentUser
, and it's normally written in the Application Event Log, but you are not giving it in your error description.
You can look it up there or you can configure topshelf to write exceptions to a log file, for example with NLog. Insert this into your app.config
straight after <configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
<nlog>
<targets>
<target name="file" type="File"
layout="${longdate} ${logger} ${message} ${exception:format=ToString}"
fileName="logfile.txt" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="file" />
</rules>
</nlog>
Add x.UseNLog();
inside your HostFactory.Run
, as described here.
Both in event log or in the logfile.txt
I'm seeing this:
System.Exception: StartProcessAsCurrentUser: CreateProcessAsUser failed. Error Code -2
at murrayju.ProcessExtensions.ProcessExtensions.StartProcessAsCurrentUser(String appPath, String cmdLine, String workDir, Boolean visible) in D:\Users\andrewsav\Documents\GitHub\CreateProcessAsUser\ProcessExtensions\ProcessExtensions.cs:line 249
at TopShelfDemo.ServiceWrapper.Start() in D:\Users\andrewsav\Documents\GitHub\CreateProcessAsUser\TopShelfDemo\Program.cs:line 58
at TopShelfDemo.Program.<>c.<Main>b__0_5(ServiceWrapper wrapper) in D:\Users\andrewsav\Documents\GitHub\CreateProcessAsUser\TopShelfDemo\Program.cs:line 21
at Topshelf.ServiceConfiguratorExtensions.<>c__DisplayClass2_0`1.<WhenStarted>b__0(T service, HostControl control)
at Topshelf.Builders.DelegateServiceBuilder`1.DelegateServiceHandle.Start(HostControl hostControl)
at Topshelf.Runtime.Windows.WindowsServiceHost.OnStart(String[] args)
So I'm getting Error 2. If you look it up here you will find out that it means "File Not Found". That is to say that CreateProcessAsUser
method cannot find calc.exe
. If you refer to CreateProcessAsUser documentation you will discover that it does not search path, so your calc.exe
expects to be in your current folder.
Topshelf apprarently sets current folder to the executable folder, I'm not sure why, some more discussion is here.
Also make sure that you are not running into 32 vs 64 bit issue as mentioned here.
I added Directory.SetCurrentDirectory(Environment.SystemDirectory);
in your example in front of ProcessExtensions.StartProcessAsCurrentUser("calc.exe");
and it worked for me.
Hi,
I was trying to write a simple wrapper like you have here with DemoService but using TopShelf. Unfortunately, I cannot get it working. What I have is very simple:
So, I'm running VS as Administrator, and if I run this, the
WTSQueryUserToken( )
call always returns zero.So, I figured maybe I have to install the service first. TopShelf builds command line params for install/uninstall/start, etc. of the service. It installs fine:
If I try to start it in the Services tool, it fails with
Windows could not start the DemoServiceTopShelf service on Local Computer. Error 5: Access is denied.
In event viewer, I see these events got logged related to it:
And this event log:
I have searched through past issues related to the Access Denied, but I don't see a concrete solution here. It is worth noting if you clone this repo and run the .bat scripts, it does work for me on the same machine.
Any insight into what might be going wrong?