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.
nightmare637
  • 635
  • 5
  • 19
  • 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? – Cee McSharpface Nov 21 '18 at 15:39
  • related: https://stackoverflow.com/a/15502739/1132334, https://stackoverflow.com/a/2205777/1132334 – Cee McSharpface Nov 21 '18 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? – nightmare637 Nov 21 '18 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](https://stackoverflow.com/questions/4237225/allow-service-to-interact-with-desktop-in-windows), since setting that flag might be a security concern (it can be set programmatically). – nilsK Nov 21 '18 at 15:55
  • nilsK, I have to have the service installed in the first place to be able to check that flag. – nightmare637 Nov 21 '18 at 16:11
  • that's quite annoying that an unchanged template would not work... are you in a windows domain environment, could it be that the "domain admin but not local admin" thing applies here (the first of the related links I posted above). which windows version is it? – Cee McSharpface Nov 21 '18 at 16:15
  • show the full output of InstallUtil, so we get an idea where in the installation process the error occurs. it might be related to registering the service in the service manager, or creating/writing to the event logs for example) – Cee McSharpface Nov 21 '18 at 16:23
  • Yes, it is very frustrating! As for your question, yes I am in a domain environment. I checked out your first link saw that the group my admin account was located in was also in the local admin group. However, just to be safe, I also added my admin account name directly to the local admin group. Unfortunately, this still did not remedy the problem. I am running this on my local Windows 10 computer. – nightmare637 Nov 21 '18 at 16:24
  • As per your request, I have edited my original post to show the full output of InstallUtil. I named my project "FailedService", if you are wondering about the name. Thank you so much for your timely assistance! – nightmare637 Nov 21 '18 at 16:37
  • Does the windows event viewer shows something ? – Kianii Nov 21 '18 at 16:56
  • It might be something to do with domain environment. Following the same steps on a 'Workgroup' PC resulted in a successful installation. – stuartd Nov 21 '18 at 16:56
  • suggestions to try: 1. make a release build and 2. copy the exe to a path outside of the local user directory (is it a roaming profile, by the way?) to something like c:\failedservice\failedservice.exe and attempt to install from there to diagnose a possible problem with the `Users` special folder. I'm doing development local machine service installs in a domain-joined similar environment all the time and never saw that error happening. but my local working folder is always something like c:\projects\... and never c:\users\... – Cee McSharpface Nov 21 '18 at 17:09
  • Kianii - Haven't checked the event viewer yet, I'll do that on Monday. stuartd - Yes, I think it's definitely something in the domain environment. dlatikay - I took the executable and put it in C:\Program Files\ as we have app locker and I know I've gotten programs to work by doing that. Unfortunately, it did not work. I didn't try a release build, so I will do that on Monday. And no, we do not use roaming profiles. – nightmare637 Nov 21 '18 at 20:28
  • Okay, I checked the event viewer and couldn't find any logs relating to the issue. Maybe I'm looking in the wrong place? The release build did not work either. One thing that has changed, though, is that now I'm getting a completely different error: "System.InvalidOperationException: Cannot open Service Control Manager on computer '.'." I googled this and every answer seems to be to just run Visual Studio Command Prompt as administrator. Already tried that, so...any new ideas? – nightmare637 Nov 26 '18 at 16:54

2 Answers2

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\

nightmare637
  • 635
  • 5
  • 19
0

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

Kianii
  • 141
  • 1
  • 9
  • how/why is that different to "I run command prompt as administrator" – Cee McSharpface Nov 21 '18 at 15:44
  • 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. – nightmare637 Nov 21 '18 at 15:48
  • 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 – Kianii Nov 21 '18 at 15:56