Reputation: 2587
So here I am again experiencing yet another MSDN fail. As usual, I've decided to learn how to write a Windows Service in C# and due to many tutorials offering incomplete or just poorly-written code, I've decided to use MSDN.
I've followed every step and my service will not install.
The page I'm following along with is: http://msdn.microsoft.com/en-us/library/zt39148a(v=vs.110).aspx
and my code is:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
namespace WindowsService1
{
public partial class WindowsService1 : ServiceBase
{
public WindowsService1()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
{
System.Diagnostics.EventLog.CreateEventSource(
"MySource", "MyNewLog");
}
eventLog1.Source = "MySource";
eventLog1.Log = "MyNewLog";
}
protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("In OnStart");
}
protected override void OnStop()
{
eventLog1.WriteEntry("In onStop.");
}
protected override void OnContinue()
{
eventLog1.WriteEntry("In OnContinue.");
}
}
}
I've gone into developer command prompt, cd'd to the directory containing my .exe (the debug folder), and I've:
installutil WindowsService1.exe
and also tried installutil.exe WindowsService1.exe
but the installation always fails, with the following text:
C:\Program Files (x86)\Microsoft Visual Studio 11.0>cd C:\Users\Tommy\Documents\ Visual Studio 2013\Projects\WindowsService1\WindowsService1\bin\Debug
C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsServ ice1\bin\Debug>installutil WindowsService1.exe Microsoft (R) .NET Framework Installation utility Version 4.0.30319.32559 Copyright (C) Microsoft Corporation. All rights reserved.
Running a transacted installation.
Beginning the Install phase of the installation. See the contents of the log file for the C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe asse mbly's progress. The file is located at C:\Users\Tommy\Documents\Visual Studio 2013\Projects\Wind owsService1\WindowsService1\bin\Debug\WindowsService1.InstallLog. Installing assembly 'C:\Users\Tommy\Documents\Visual Studio 2013\Projects\Window sService1\WindowsService1\bin\Debug\WindowsService1.exe'. Affected parameters are: logtoconsole = logfile = C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService 1\WindowsService1\bin\Debug\WindowsService1.InstallLog assemblypath = C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsSe rvice1\WindowsService1\bin\Debug\WindowsService1.exe Installing service Service1... Creating EventLog source Service1 in log Application...
An exception occurred during the Install phase. System.InvalidOperationException: Cannot open Service Control Manager on compute r '.'. This operation might require other privileges. The inner exception System.ComponentModel.Win32Exception was thrown with the fol lowing error message: Access is denied.
The Rollback phase of the installation is beginning. See the contents of the log file for the C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe asse mbly's progress. The file is located at C:\Users\Tommy\Documents\Visual Studio 2013\Projects\Wind owsService1\WindowsService1\bin\Debug\WindowsService1.InstallLog. Rolling back assembly 'C:\Users\Tommy\Documents\Visual Studio 2013\Projects\Wind owsService1\WindowsService1\bin\Debug\WindowsService1.exe'. Affected parameters are: logtoconsole = logfile = C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService 1\WindowsService1\bin\Debug\WindowsService1.InstallLog assemblypath = C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsSe rvice1\WindowsService1\bin\Debug\WindowsService1.exe Restoring event log to previous state for source Service1.
The Rollback phase completed successfully.
The transacted install has completed. The installation failed, and the rollback has been performed.
C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsServ ice1\bin\Debug>installutil WindowsService1 Microsoft (R) .NET Framework Installation utility Version 4.0.30319.32559 Copyright (C) Microsoft Corporation. All rights reserved.
Exception occurred while initializing the installation: System.IO.FileNotFoundException: Could not load file or assembly 'file:///C:\Use rs\Tommy\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsService1\b in\Debug\WindowsService1' or one of its dependencies. The system cannot find the file specified..
C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsServ ice1\bin\Debug>installutil WindowsService1.exe Microsoft (R) .NET Framework Installation utility Version 4.0.30319.32559 Copyright (C) Microsoft Corporation. All rights reserved.
Running a transacted installation.
Beginning the Install phase of the installation. See the contents of the log file for the C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe asse mbly's progress. The file is located at C:\Users\Tommy\Documents\Visual Studio 2013\Projects\Wind owsService1\WindowsService1\bin\Debug\WindowsService1.InstallLog. Installing assembly 'C:\Users\Tommy\Documents\Visual Studio 2013\Projects\Window sService1\WindowsService1\bin\Debug\WindowsService1.exe'. Affected parameters are: logtoconsole = logfile = C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService 1\WindowsService1\bin\Debug\WindowsService1.InstallLog assemblypath = C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsSe rvice1\WindowsService1\bin\Debug\WindowsService1.exe Installing service Service1... Creating EventLog source Service1 in log Application...
An exception occurred during the Install phase. System.InvalidOperationException: Cannot open Service Control Manager on compute r '.'. This operation might require other privileges. The inner exception System.ComponentModel.Win32Exception was thrown with the fol lowing error message: Access is denied.
The Rollback phase of the installation is beginning. See the contents of the log file for the C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe asse mbly's progress. The file is located at C:\Users\Tommy\Documents\Visual Studio 2013\Projects\Wind owsService1\WindowsService1\bin\Debug\WindowsService1.InstallLog. Rolling back assembly 'C:\Users\Tommy\Documents\Visual Studio 2013\Projects\Wind owsService1\WindowsService1\bin\Debug\WindowsService1.exe'. Affected parameters are: logtoconsole = logfile = C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService 1\WindowsService1\bin\Debug\WindowsService1.InstallLog assemblypath = C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsSe rvice1\WindowsService1\bin\Debug\WindowsService1.exe Restoring event log to previous state for source Service1.
The Rollback phase completed successfully.
The transacted install has completed. The installation failed, and the rollback has been performed.
C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsServ ice1\bin\Debug>
which also outputs an INSTALLLOG file into theDebug directory, which contains (the same info):
Running a transacted installation.
Beginning the Install phase of the installation. See the contents of the log file for the C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe assembly's progress. The file is located at C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.InstallLog.
An exception occurred during the Install phase. System.InvalidOperationException: Cannot open Service Control Manager on computer '.'. This operation might require other privileges. The inner exception System.ComponentModel.Win32Exception was thrown with the following error message: Access is denied.
The Rollback phase of the installation is beginning. See the contents of the log file for the C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe assembly's progress. The file is located at C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.InstallLog.
The Rollback phase completed successfully.
The transacted install has completed.
Running a transacted installation.
Beginning the Install phase of the installation. See the contents of the log file for the C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe assembly's progress. The file is located at C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.InstallLog.
An exception occurred during the Install phase. System.InvalidOperationException: Cannot open Service Control Manager on computer '.'. This operation might require other privileges. The inner exception System.ComponentModel.Win32Exception was thrown with the following error message: Access is denied.
The Rollback phase of the installation is beginning. See the contents of the log file for the C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe assembly's progress. The file is located at C:\Users\Tommy\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.InstallLog.
The Rollback phase completed successfully.
The transacted install has completed.
So then I go back to MSDN. To see what to do if the installation fails, but instead I see this:
If the service installs successfully, installutil.exe will report success.
There is no other information about what to do if the install fails.
I've spent days on this and I've followed over 8 tutorials, all of which have either incomplete or bad code (that doesn't work). So I finally decide to put my trust in MSDN again but as usual I'm left wondering what the hell is going on.
My user account has full read/write/exec permissions, is an administrator and I have checked Computer Management (> Services and Applications) and confirmed that my Windows Service is not listed there.
What do I do now? How do I create a Windows Service (that works)?
Upvotes: 0
Views: 3232
Reputation: 3876
An exception occurred during the Install phase. System.InvalidOperationException: Cannot open Service Control Manager on compute r '.'. This operation might require other privileges. The inner exception System.ComponentModel.Win32Exception was thrown with the fol lowing error message: Access is denied.
The error says what the problem is: you don't have permission to install the service.
If the machine is a domain member, it's entirely possible that the local admin account doesn't have permissions required to install the service.
It's also possible that it's being blocked by AV software or that your service needs to be added to a whitelist. The possibilities are nearly endless, so you'll need to find out what the specific error is.
You might get more information from the Security Event Log. You can also try the sysinternals Process Monitor. It should show you what it's trying to do and what is failing.
My user account has full read/write/exec permissions, is an administrator
If you're on a domain, you may need to be a domain admin.
Upvotes: 1
Reputation: 27214
An exception occurred during the Install phase. System.InvalidOperationException: Cannot open Service Control Manager on computer '.'. This operation might require other privileges.
Run your developer command prompt "as administrator". Being an administrator is not the same as running your processes as administrator.
From MSDN:
On the Start menu or Start Page, open the shortcut menu for Developer Command Prompt, and then choose Run As Administrator.
Upvotes: 4