Windows services follow a specific startup sequence based on their dependencies and configuration. The basic order is:
- System services (Session Manager, Registry, etc.)
- Network services (DHCP Client, DNS Client, Network Location Awareness)
- Dependent services (services that rely on the above)
Your service is attempting TCP connections before network interfaces are ready, which explains the hanging behavior. This is a common pitfall when services don't properly declare their dependencies.
Here are three ways to address this:
// 1. Setting dependencies in ServiceInstaller (C#)
protected override void OnBeforeInstall(IDictionary savedState)
{
base.OnBeforeInstall(savedState);
using (ServiceController sc = new ServiceController("MyService"))
{
string[] dependencies = { "Tcpip", "Dhcp", "Nla" };
ServiceInstaller installer = new ServiceInstaller();
installer.ServicesDependedOn = dependencies;
}
}
Or through PowerShell for existing services:
# 2. PowerShell command to set dependencies
Set-Service -Name "YourService" -DependenciesOn @("nsi","Tcpip","Dhcp")
While delayed start can work, it's not the optimal solution:
# 3. Setting delayed start via registry
New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\YourService"
-Name "DelayedAutostart" -Value 1 -PropertyType DWORD
Implement network readiness verification in your service code:
// C# example for network check
private bool IsNetworkReady()
{
return NetworkInterface.GetIsNetworkAvailable() &&
System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties()
.GetActiveTcpConnections().Any();
}
protected override void OnStart(string[] args)
{
while (!IsNetworkReady())
{
Thread.Sleep(5000);
}
// Proceed with service initialization
}
Always configure proper recovery options through SC command:
sc failure "YourService" reset= 86400 actions= restart/60000/restart/60000/restart/60000
The key is proper dependency declaration rather than just delaying startup. This ensures deterministic service initialization order while maintaining system stability.
During Windows Server boot sequence, services follow a specific startup order based on their dependencies. Critical network services like DHCP Client (Dhcp), DNS Client (Dnscache), and Network Connections (Netman) typically start early in the boot process. Services that depend on network connectivity should be configured to start after these dependencies are available.
To check your service's current dependencies, run this PowerShell command:
Get-Service -Name "YourServiceName" | Select-Object -Property Name, DisplayName, @{ Name="Dependencies"; Expression={(Get-Item -Path "HKLM:\SYSTEM\CurrentControlSet\Services\$($_.Name)").GetValue("DependOnService")} }
For TCP-dependent services, delayed start (START_DELAYED_AUTOSTART) is often the right solution. This can be configured via Registry:
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\YourServiceName] "DelayedAutostart"=dword:00000001
Or programmatically in C#:
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\YourServiceName", true)) { key.SetValue("DelayedAutostart", 1, RegistryValueKind.DWord); }
For more reliable behavior than delayed start alone, explicitly declare dependencies in your service's installer code (C# example):
ServiceInstaller serviceInstaller = new ServiceInstaller(); serviceInstaller.ServiceName = "YourServiceName"; serviceInstaller.ServicesDependedOn = new string[] { "Tcpip", "Dhcp", "Dnscache", "Netman" };
Even with proper startup ordering, adding network availability verification in your service provides additional robustness:
private bool IsNetworkAvailable() { try { return NetworkInterface.GetIsNetworkAvailable() && System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties() .GetActiveTcpConnections().Any(); } catch { return false; } }
If your service still hangs during boot, enable service debug logging to a file:
protected override void OnStart(string[] args) { try { File.AppendAllText(@"C:\ServiceDebug.log", $"[{DateTime.Now}] Service starting. Network available: {IsNetworkAvailable()}\n"); // Your startup code here } catch (Exception ex) { File.AppendAllText(@"C:\ServiceDebug.log", $"[{DateTime.Now}] Startup failed: {ex}\n"); throw; } }