Install Windows Service created in Visual Studio
Asked Answered
D

8

148

When I create a new Windows Service in Visual Studio 2010, I get the message stating to use InstallUtil and net start to run the service.

I have tried the following steps:

  1. Create new project File -> New -> Project -> Windows Service
  2. Project Name: TestService
  3. Build project as is (Service1 constructor, OnStart, OnStop)
  4. Open command prompt, run "C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe" TestService.exe
  5. Run net start TestService.

Output of step 4

Running a transacted installation.

Beginning the Install phase of the installation.

See the contents of the log file for the C:\Users\myusername\Documents\Visual Studio 2010\Projects\TestService\TestService\obj\x86\Debug\TestService.exe assembly's progress.

The file is located at C:\Users\myusername\Documents\Visual Studio 2010\Projects\Tes tService\TestService\obj\x86\Debug\TestService.InstallLog.

Installing assembly 'C:\Users\myusername\Documents\Visual Studio 2010\Projects\TestS ervice\TestService\obj\x86\Debug\TestService.exe'.

Affected parameters are:

logtoconsole =

logfile = C:\Users\myusername\Documents\Visual Studio 2010\Projects\TestService\T estService\obj\x86\Debug\TestService.InstallLog

assemblypath = C:\Users\myusername\Documents\Visual Studio 2010\Projects\TestServ ice\TestService\obj\x86\Debug\TestService.exe

No public installers with the RunInstallerAttribute.Yes attribute could be found in the C:\Users\myusername\Documents\Visual Studio 2010\Projects\TestService\TestSe rvice\obj\x86\Debug\TestService.exe assembly.

The Install phase completed successfully, and the Commit phase is beginning.

See the contents of the log file for the C:\Users\myusername\Documents\Visual Studio 2010\Projects\TestService\TestService\obj\x86\Debug\TestService.exe assembly's progress.

The file is located at C:\Users\myusername\Documents\Visual Studio 2010\Projects\Tes tService\TestService\obj\x86\Debug\TestService.InstallLog.

Committing assembly 'C:\Users\myusername\Documents\Visual Studio 2010\Projects\TestS ervice\TestService\obj\x86\Debug\TestService.exe'.

Affected parameters are:

logtoconsole =

logfile = C:\Users\myusername\Documents\Visual Studio 2010\Projects\TestService\T estService\obj\x86\Debug\TestService.InstallLog

assemblypath = C:\Users\myusername\Documents\Visual Studio 2010\Projects\TestServ ice\TestService\obj\x86\Debug\TestService.exe

No public installers with the RunInstallerAttribute.Yes attribute could be found in the C:\Users\myusername\Documents\Visual Studio 2010\Projects\TestService\TestSe rvice\obj\x86\Debug\TestService.exe assembly.

Remove InstallState file because there are no installers.

The Commit phase completed successfully.

The transacted install has completed.

Output of step 5

The service name is invalid.

More help is available by typing NET HELPMSG 2185.

Dilemma answered 27/10, 2011 at 20:41 Comment(0)
Z
261

You need to open the Service.cs file in the designer, right click it and choose the menu-option "Add Installer".

It won't install right out of the box... you need to create the installer class first.

Some reference on service installer:

How to: Add Installers to Your Service Application

Quite old... but this is what I am talking about:

Windows Services in C#: Adding the Installer (part 3)

By doing this, a ProjectInstaller.cs will be automaticaly created. Then you can double click this, enter the designer, and configure the components:

  • serviceInstaller1 has the properties of the service itself: Description, DisplayName, ServiceName and StartType are the most important.

  • serviceProcessInstaller1 has this important property: Account that is the account in which the service will run.

For example:

this.serviceProcessInstaller1.Account = ServiceAccount.LocalSystem;
Zorina answered 27/10, 2011 at 20:45 Comment(5)
Adding the installer and setting the account to LocalSystem did it. Thanks!Latonia
I am getting the same error in VS2013. I checked the links you provided, verified that I have ProjectInstaller, including the components service[Process]Installer1, properly configured. I run installutil.exe as Administrator. It still reports "No public installers with the RunInstallerAttribute.Yes attribute could be found". Any ideas?Acetamide
Lol. I love that the "Quite old" link is to a site called Arcane Code. The older that page gets, the truer the name becomes :-)Beholden
What is "the designer"? Applications that have no UI generally don't involve anything that would be called a designer.Quartis
Services also have a designer, just like formsSlaw
T
12

Looking at:

No public installers with the RunInstallerAttribute.Yes attribute could be found in the C:\Users\myusername\Documents\Visual Studio 2010\Projects\TestService\TestSe rvice\obj\x86\Debug\TestService.exe assembly.

It looks like you may not have an installer class in your code. This is a class that inherits from Installer that will tell installutil how to install your executable as a service.

P.s. I have my own little self-installing/debuggable Windows Service template here which you can copy code from or use: Debuggable, Self-Installing Windows Service

Traitorous answered 27/10, 2011 at 20:45 Comment(2)
When I right click the TestService project in Visual Studio -> Properties -> Services, the page is disabled...were you referring to a different location? Under Application, the Assembly name is TestService.Dilemma
@John: Ignore the first part about the services console, look at the second part starting with Actually. It looks like the service never installed because it didn't find an installer.Traitorous
P
10

Here is an alternate way to make the installer and get rid of that error message. Also it seems that VS2015 express does not have the "Add Installer" menu item.

You simply need to create a class and add the below code and add the reference System.Configuration.Install.dll.

using System.Configuration.Install;
using System.ServiceProcess;
using System.ComponentModel;


namespace SAS
{
    [RunInstaller(true)]
    public class MyProjectInstaller : Installer
    {
        private ServiceInstaller serviceInstaller1;
        private ServiceProcessInstaller processInstaller;

        public MyProjectInstaller()
        {
            // Instantiate installer for process and service.
            processInstaller = new ServiceProcessInstaller();
            serviceInstaller1 = new ServiceInstaller();

            // The service runs under the system account.
            processInstaller.Account = ServiceAccount.LocalSystem;

            // The service is started manually.
            serviceInstaller1.StartType = ServiceStartMode.Manual;

            // ServiceName must equal those on ServiceBase derived classes.
            serviceInstaller1.ServiceName = "SAS Service";

            // Add installer to collection. Order is not important if more than one service.
            Installers.Add(serviceInstaller1);
            Installers.Add(processInstaller);
        }
    }
}
Perfusion answered 3/11, 2016 at 19:48 Comment(1)
Running VS2015 as well and this solution got me past the "No public installers with the RunInstallerAttribute.Yes " error message that I was previously getting. Thank you!Intenerate
R
9

Two typical problems:

  1. Missing the ProjectInstaller class (as @MiguelAngelo has pointed)
  2. The command prompt must “Run as Administrator
Recliner answered 29/8, 2015 at 21:38 Comment(0)
G
4

Another possible problem (which I ran into):

Be sure that the ProjectInstaller class is public. To be honest, I am not sure how exactly I did it, but I added event handlers to ProjectInstaller.Designer.cs, like:

this.serviceProcessInstaller1.BeforeInstall += new System.Configuration.Install.InstallEventHandler(this.serviceProcessInstaller1_BeforeInstall);

I guess during the automatical process of creating the handler function in ProjectInstaller.cs it changed the class definition from

public class ProjectInstaller : System.Configuration.Install.Installer

to

partial class ProjectInstaller : System.Configuration.Install.Installer

replacing the public keyword with partial. So, in order to fix it it must be

public partial class ProjectInstaller : System.Configuration.Install.Installer

I use Visual Studio 2013 Community edition.

Gula answered 28/7, 2016 at 12:18 Comment(1)
I know this is 3 years later, but this solved my problem. Not sure why or when the designer changed the previous public partial class to internal partial class. Thank you!Appraisal
P
2

Stealth Change in VS 2010 and .NET 4.0 and Later

No public installers with the RunInstallerAttribute.Yes attribute could be found

There is an alias change or compiler cleanup in .NET that may reveal this little tweak for your specific case.

If you have the following code …

RunInstaller(true)   // old alias  

You may need to update it to

RunInstallerAttribute(true)  // new property spelling

It is like an alias changed under the covers at compile time or at runtime and you will get this error behavior. The above explicit change to RunInstallerAttribute(true) fixed it in all of our install scenarios on all machines.

After you add project or service installer then check for the “old” RunInstaller(true) and change it to the new RunInstallerAttribute(true)

Psoas answered 22/1, 2017 at 18:2 Comment(1)
As per my knowledge you can omit the "Attribute" postfix, i.e. [RunInstaller(true)] and [RunInstallerAttribute(true)] is the same. Try it with a different attribute, e.g. [DebuggerStepThrough()] and [DebuggerStepThroughAttribute()] - both will work when applied to a class.Buster
I
1

Yet another catch I ran into: ensure your Installer derived class (typically ProjectInstaller) is at the top of the namespace hierarchy, I tried to use a public class within another public class, but this results in the same old error:

No public installers with the RunInstallerAttribute.Yes attribute could be found

Imhoff answered 26/3, 2019 at 14:9 Comment(0)
S
0

Or just use sc.exe to add (create switch) or remove (delete switch).

Silica answered 25/7, 2023 at 12:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.