Install Windows Service created in Visual Studio

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:

  • Create new project File -> New -> Project -> Windows Service
  • Project Name: TestService
  • Build project as is (Service1 constructor, OnStart, OnStop)
  • Open command prompt, run "C:WindowsMicrosoft.NETFrameworkv4.0.30319InstallUtil.exe" TestService.exe
  • 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:UsersmyusernameDocumentsVisual Studio 2010ProjectsTestServiceTestServiceobjx86DebugTestService.exe assembly's progress.

    The file is located at C:UsersmyusernameDocumentsVisual Studio 2010ProjectsTes tServiceTestServiceobjx86DebugTestService.InstallLog.

    Installing assembly 'C:UsersmyusernameDocumentsVisual Studio 2010ProjectsTestS erviceTestServiceobjx86DebugTestService.exe'.

    Affected parameters are:

    logtoconsole =

    logfile = C:UsersmyusernameDocumentsVisual Studio 2010ProjectsTestServiceT estServiceobjx86DebugTestService.InstallLog

    assemblypath = C:UsersmyusernameDocumentsVisual Studio 2010ProjectsTestServ iceTestServiceobjx86DebugTestService.exe

    No public installers with the RunInstallerAttribute.Yes attribute could be found in the C:UsersmyusernameDocumentsVisual Studio 2010ProjectsTestServiceTestSe rviceobjx86DebugTestService.exe assembly.

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

    See the contents of the log file for the C:UsersmyusernameDocumentsVisual Studio 2010ProjectsTestServiceTestServiceobjx86DebugTestService.exe assembly's progress.

    The file is located at C:UsersmyusernameDocumentsVisual Studio 2010ProjectsTes tServiceTestServiceobjx86DebugTestService.InstallLog.

    Committing assembly 'C:UsersmyusernameDocumentsVisual Studio 2010ProjectsTestS erviceTestServiceobjx86DebugTestService.exe'.

    Affected parameters are:

    logtoconsole =

    logfile = C:UsersmyusernameDocumentsVisual Studio 2010ProjectsTestServiceT estServiceobjx86DebugTestService.InstallLog

    assemblypath = C:UsersmyusernameDocumentsVisual Studio 2010ProjectsTestServ iceTestServiceobjx86DebugTestService.exe

    No public installers with the RunInstallerAttribute.Yes attribute could be found in the C:UsersmyusernameDocumentsVisual Studio 2010ProjectsTestServiceTestSe rviceobjx86DebugTestService.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.


    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 thas is the account in wich the service will run.

  • For example:

    this.serviceProcessInstaller1.Account = ServiceAccount.LocalSystem;
    

    Looking at:

    No public installers with the RunInstallerAttribute.Yes attribute could be found in the C:UsersmyusernameDocumentsVisual Studio 2010ProjectsTestServiceTestSe rviceobjx86DebugTestService.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.

    Ps 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


    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);
            }
        }
    }
    
    链接地址: http://www.djcxy.com/p/44214.html

    上一篇: Visual Studio发布生成事件返回错误MSB3073

    下一篇: 安装在Visual Studio中创建的Windows服务