60

I am attempting to install a C# windows service project using a VisualStudio.Net deployment project.

To run the deployment project I right-click and select "install" from the context menu, the install wizard runs and eventually prompts me with a "Set Service Login" dialog which asks for username & password.

When I install a service using the sc utility from the command line, I don't have to provide credentials.

Do I have to create a login just for this service? I'd prefer to use "Local System" or "Network Service" (not sure what the difference is) as other services do.

Keith
  • 2,618
  • 4
  • 29
  • 44

5 Answers5

114

Add this code to your private void InitializeComponent() method in projectInstaller.Designer.cs file in your windows service project.

this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;

if the definition of you process installer is:

private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
anthares
  • 11,070
  • 4
  • 41
  • 61
  • @anthares I need to run the service as current user, how can I fix that? I use: ServiceAccount.User but get the password request when Install it... – Zhenya Mar 12 '15 at 09:50
  • 4
    As the code spot for that is in designer-generated code I just did the same thing by opening the designer, getting properties on the serviceProcessInstaller1 object, then selecting LocalSystem in the drop-down for Account. – Mike K May 05 '15 at 20:43
  • I prefer @David 's answer below, using the designer.... Does roughly the same thing, but I think that method is better. – mike Jun 13 '16 at 22:19
  • and remember to rebuild – West Aug 10 '21 at 01:11
24

Check this Link: http://msdn.microsoft.com/en-us/library/zt39148a(v=vs.110).aspx

Pay attention to this section: To create the installers for your service

Make changes to your ServiceProcessInstaller:

In the designer, click ServiceProcessInstaller1 for a Visual Basic project, or serviceProcessInstaller1 for a Visual C# project. Set the Account property to LocalSystem. This will cause the service to be installed and to run on a local service account.

David
  • 1,646
  • 17
  • 22
4
  1. Open your ProjectInstaller
  2. Right click the ServiceProcessInstaller and choose properties
  3. From the Account drop-down, under Misc, select the account you want your service to run as

For details of the different accounts and their privileges see the following link:

http://msdn.microsoft.com/en-us/library/system.serviceprocess.serviceaccount.aspx

Adam Cooper
  • 8,077
  • 2
  • 33
  • 51
4

In the project that contains the service, add an Installer class. Make it look something like this:

[RunInstaller(true)]
public class MyServiceInstaller : Installer
{
    public MyServiceInstaller()
    {
        ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller();
        serviceProcessInstaller.Account = ServiceAccount.LocalSystem; // Or whatever account you want

        var serviceInstaller = new ServiceInstaller
        {
            DisplayName = "Insert the display name here",
            StartType = ServiceStartMode.Automatic, // Or whatever startup type you want
            Description = "Insert a description for your service here",
            ServiceName = "Insert the service name here"
        };

        Installers.Add(_serviceProcessInstaller);
        Installers.Add(serviceInstaller);
    }

    public override void Commit(IDictionary savedState)
    {
        base.Commit(savedState);

        // This will automatically start your service upon completion of the installation.
        try
        {
            var serviceController = new ServiceController("Insert the service name here");
            serviceController.Start();
        }
        catch
        {
            MessageBox.Show(
                "Insert a message stating that the service couldn't be started, and that the user will have to do it manually");
        }
    }
}

Then, in the solution explorer, right-click on the deployment project and select "View > Custom Actions". Right-click on Custom Actions, and select "Add Custom Action..." Pick the Application Folder and select the primary output of the project that contains the service. Now the custom actions (Commit from above) will be executed upon installation. You can add the additional methods (Install, Rollback, Uninstall) if you need other custom actions.

fre0n
  • 1,885
  • 1
  • 20
  • 26
  • I don't get it ? How this is going to help him, to set the system account to be executer of the service ? – anthares Feb 12 '10 at 16:27
  • That's what I get for copying code from my project. {facepalm} In the `Install` method of my project's installer, I was setting the 'Account` property. It's fixed now... – fre0n Feb 12 '10 at 16:32
0

The simplest way is click on your serviceProjectInstaller and set it on properties window.

enter image description here

Iván Kollár
  • 39
  • 1
  • 3