开发者

How to make Windows Service start as "Automatic (Delayed Start)"

Scenario:

A WCF service running as a Windows Service. Account is "User".


What is done:

I have overridden the OnBeforeInstall in the projectinstaller to be able to set username and password from a config file.


What I would be able to do:

I'd like to be able to set the starttype as Automatic (Delayed Start)


What I have tried:

I put the following coderow in the overridden OnBeforeInstall

serviceInstaller1.StartType = ServiceStartMode.Automatic + 1;

Figured I would trick the ServiceStartMode enum into representing Automatic (Delayed Start), didn't work. Haven't tried anything more simply because I couldn't find anything to try.


What I have found on the net:

I found out that Automatic (Delayed Start) will be available in .NET 4, but that doesn't help me right now. MSDN I found out that DelayedAutoStart could be added to the service's configuration key, but this feels like 开发者_如何学Goa hack if I should do this from code. But maybe this is the only solution available for me at this point?

  • WS2008: Startup Processes and Delayed Automatic Start

Any ideas?

Robert Persson, Sweden


Now that .NET 4.0 is here:

serviceInstaller1.StartType = ServiceStartMode.Automatic;
serviceInstaller1.DelayedAutoStart = true;


Your only other option is to use P/invoke to call ChangeServiceConfig2 with SERVICE_CONFIG_DELAYED_AUTO_START_INFO. But since you seem to be unwilling to add the registry entry, I doubt you would want to use P/invoke. There's no other way to do it from the .NET Framework (< 4.0).


For my .NET Framework 3.5 project, I can install my service as an "Automatic (Delayed)" service by manually setting the DelayedAutostart value for my service. For example:

public ProjectInstaller()
{
  ...

  AfterInstall += ProjectInstaller_AfterInstall;
}

void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e)
{
  string serviceName = <YourSpecific>Installer.ServiceName;

  using (RegistryKey serviceKey = Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\Services\" + serviceName, true))
  {
      serviceKey.SetValue("DelayedAutostart", 1, RegistryValueKind.DWord);
  }
}

Note that after you install the service, the service will not be listed as "Automatic (Delayed)" until after the computer is restarted.


I'll expand on jdknight answer a little bit. I had writting permission issues while attempting his solution, so here's what I did:

void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e)
{
    try
    {
        RegistryKey key = Registry.LocalMachine.OpenSubKey("System", true); //Opens the System hive with writting permissions set to true
        key = key.CreateSubKey("CurrentControlSet"); //CreateSubKey opens if subkey exists, otherwise it will create that subkey
        key = key.CreateSubKey("services");
        key = key.CreateSubKey(serviceInstaller1.ServiceName);
        key.SetValue("DelayedAutostart", 1, RegistryValueKind.DWord);
    }
    catch (Exception exc)
    {
        Console.WriteLine(exc.Message);
    }
}

I also registered to the AfterInstall event by adding a new instance of InstallEventHandler. I'm not sure if that's actually necessary, but it won't hurt either:

AfterInstall += new InstallEventHandler(ProjectInstaller_AfterInstall);

Works like a charm on .NET Framework 2.0. As it has been pointed out before, for frameworks 4 and above, use

serviceInstaller1.DelayedAutoStart = true;

according to fiat's answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜