'Creating new AppDomain instances inside a Windows service doesn't work
I am trying to create a Windows service with C# which spawns multiple processes inside it.
For this I am using AppDomain functionality and creating multiple of them at startup in the OnStart method of my service:
public partial class MyService : ServiceBase
{
public ServiceBase()
{
InitializeComponents();
}
protected override void OnStart(string[] args)
{
var setup = new AppDomainSetup();
setup.ApplicationBase = Path.GetDirectoryName(GetMyExePath());
var ad = AppDomain.CreateDomain("Domain NEW", null, setup);
var t = new Thread(() => {
ad.ExecuteAssembly(GetMyExePath());
AppDomain.Unload(ad);
});
t.Start();
}
}
(I know I should put a check to prevent recursively starting new domains from each new one, but for now it doesn't even start the first one)
When I try the same in a Console Application, it works fine. But when I build it as a Windows Service and run it from the service manager, it seems to terminate after startup.
Opening the Event Viewer, I see this about my service:
"Service can not be started. An instance of the service is already running."
Running it without the app domain creation code works just fine. It fails to start even a single domain as it seems to view them as trying to start the same process twice instead making a new domain within the process.
Is there a way to get this to run on services or does it only work for regular applications?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
