2

So I am trying to create a service using C# in Visual Studio 2017 and I keep getting an error when I try to install it.

Right now, I don't even have anything in my service, I just want to be able to install the service. I created a new Project in Visual Studio 2017 using the Windows Service (.NET Framework) template under Visual C# > Windows Desktop. I then added the installer via Right Click > Add Installer. For serviceProcessInstaller1, I set the account to LocalSystem. And...that's it!

Now, I try to install it. I open up command prompt (run as administrator) and I do

InstallUtil ServiceTest.exe 

Things start off smoothly, but then I get a message:

"An exception occured during the Install phase. System.UnauthorizedAccessException: Attempted to perform an unauthorized operation."

What could be generating this message?

I've googled and tried the following things:

  • Ensure I run command prompt as administrator.
  • Assign Full Control to the project folder to my admin account.
  • Configure permissions to Full Control for admin account in the security log of regedit.
  • Log into my computer as an administrator (rather than regular user) and run it that way.

Despite these things, I keep getting the same error. What else could I be doing wrong? Would really appreciate some advice!

Note: I am trying to install this service locally on my computer.

Edit: Per request, here are the logs. I named my project "FailedService", by the way, since I can't get it to work!

InstallUtil.InstallLog

Running a transacted installation. Beginning the Install phase of the installation. See the contents of the log file for the C:\Users\first.m.last\source\repos\FailedService\FailedService\bin\Debug\FailedService.exe assembly's progress. The file is located at C:\Users\first.m.last\source\repos\FailedService\FailedService\bin\Debug\FailedService.InstallLog. An exception occurred during the Install phase. System.UnauthorizedAccessException: Attempted to perform an unauthorized operation. The Rollback phase of the installation is beginning. See the contents of the log file for the C:\Users\first.m.last\source\repos\FailedService\FailedService\bin\Debug\FailedService.exe assembly's progress. The file is located at C:\Users\first.m.last\source\repos\FailedService\FailedService\bin\Debug\FailedService.InstallLog. The Rollback phase completed successfully. The transacted install has completed. 

FailedService.InstallLog

Installing assembly 'C:\Users\first.m.last\source\repos\FailedService\FailedService\bin\Debug\FailedService.exe'. Affected parameters are: logtoconsole = logfile = C:\Users\first.m.last\source\repos\FailedService\FailedService\bin\Debug\FailedService.InstallLog assemblypath = C:\Users\first.m.last\source\repos\FailedService\FailedService\bin\Debug\FailedService.exe Installing service Service1... Creating EventLog source Service1 in log Application... Rolling back assembly 'C:\Users\first.m.last\source\repos\FailedService\FailedService\bin\Debug\FailedService.exe'. Affected parameters are: logtoconsole = logfile = C:\Users\first.m.last\source\repos\FailedService\FailedService\bin\Debug\FailedService.InstallLog assemblypath = C:\Users\first.m.last\source\repos\FailedService\FailedService\bin\Debug\FailedService.exe Restoring event log to previous state for source Service1. 
14
  • could you show the parts of your code where you override the oninstallation method to set the user account to localsystem? try leaving that out at first, does it work then? Commented Nov 21, 2018 at 15:39
  • related: stackoverflow.com/a/15502739/1132334, stackoverflow.com/a/2205777/1132334 Commented Nov 21, 2018 at 15:49
  • I don't think I overrode the oninstallation method... To set the user account to local system, I had ProjectInstaller.cs open in Design View, I left clicked on serviceProcessInstaller1, and in the properties box beneath Misc, I set Account to "LocalSystem". Not sure if it matters, but I tried LocalService and NetworkService and received the same results. Would you still like to see my code - I left everything as default and did not make any modifications other than what I mentioned? Commented Nov 21, 2018 at 15:52
  • Not sure, what your service does, simple guessing ... there is a check-box for installed services, that 'Allow service to interact with desktop'. Please read this post, since setting that flag might be a security concern (it can be set programmatically). Commented Nov 21, 2018 at 15:55
  • nilsK, I have to have the service installed in the first place to be able to check that flag. Commented Nov 21, 2018 at 16:11

2 Answers 2

1

I was FINALLY able to resolve this issue. I was able to get to the bottom of it by using SysInternal's Process Monitor. I opened up the program and had it take logs for InstallUtil.exe. From there, I began to search for any logs that said "Access Denied". As I sorted through them, I noticed something interesting: it was trying to create a registry key, but it kept failing!

So I went into the registry and created a key named "ServiceTest". After that, I ran the InstallUtil command again, and it worked!

If anyone else is having trouble, the solution is to create a key named after your process name in the following location:

HKLM\System\CurrentControlSet\Services\EventLog\Application\

Sign up to request clarification or add additional context in comments.

Comments

0

Enter the full path to installUtil & your service binary in an elevated prompt, that could do.

3 Comments

how/why is that different to "I run command prompt as administrator"
As dlatikay said, I am already running the prompt as administrator. Also, I was running installUtil from the full path to begin with. I was not running the service binary in the full path since I was already working from that directory to begin with. However, I did try running both from the full path and it still did not work.
The point is not in "as administrator" but in "full path". it's a well known bug that i aslo encountered myself. but anyway, it did not work

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.