Windows Service - UnauthorizedAccessException Error when Installing Service (C#)

2.6k views Asked by At

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.
2

There are 2 answers

0
nightmare637 On BEST ANSWER

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\

3
Kianii On

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