Skip to content

Commit

Permalink
Workaround dotnet issue 50020: Windows container service bug
Browse files Browse the repository at this point in the history
  • Loading branch information
spaceisfun committed Apr 27, 2021
1 parent 07d1a29 commit 51c113e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
2 changes: 1 addition & 1 deletion build/internal/FactoryOrchestratorServiceTemplate.wm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
description="Factory Orchestrator Service"
displayName="Factory Orchestrator Service"
errorControl="normal"
imagePath="%systemroot%\system32\manufacturing\FactoryOrchestrator\Microsoft.FactoryOrchestrator.Service.exe action:run"
imagePath="%systemroot%\system32\manufacturing\FactoryOrchestrator\Microsoft.FactoryOrchestrator.Service.exe -IsService"
name="Microsoft.FactoryOrchestrator.Service"
objectName="LocalSystem"
start="auto"
Expand Down
2 changes: 1 addition & 1 deletion install/InstallFactoryOrchestratorService.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ else
}
else
{
$null = New-Service -Name "Microsoft.FactoryOrchestrator" -BinaryPathName "$installdir\Microsoft.FactoryOrchestrator.Service.exe" -Description "Factory Orchestrator service version $Version$" -StartupType Manual
$null = New-Service -Name "Microsoft.FactoryOrchestrator" -BinaryPathName "$installdir\Microsoft.FactoryOrchestrator.Service.exe -IsService" -Description "Factory Orchestrator service version $Version$" -StartupType Manual
}

Write-Host "Factory Orchestrator service version $Version$ is installed to `"$installdir`" and configured as a Windows service!`n"
Expand Down
48 changes: 37 additions & 11 deletions src/Service/ServiceExe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license.

using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
Expand All @@ -16,7 +16,9 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Hosting.WindowsServices;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.EventLog;
using Microsoft.FactoryOrchestrator.Client;
using Microsoft.FactoryOrchestrator.Core;
using Microsoft.FactoryOrchestrator.Server;
Expand Down Expand Up @@ -106,19 +108,43 @@ public static IHost CreateIpcHost(bool allowNetworkAccess, int port, X509Certifi
}).Build();

public static void Main(string[] args)
{
Host.CreateDefaultBuilder(null).UseSystemd().UseWindowsService().ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Worker>();
}).ConfigureLogging(builder =>
{
{
#if DEBUG
var _logLevel = LogLevel.Debug;
var _logLevel = LogLevel.Debug;
#else
var _logLevel = LogLevel.Information;
var _logLevel = LogLevel.Information;
#endif
builder.SetMinimumLevel(_logLevel).AddConsole().AddProvider(new LogFileProvider());
}).Build().Run();
var hostBuilder = Host.CreateDefaultBuilder(null).UseSystemd().ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Worker>();
}).ConfigureLogging(builder =>
{
builder.SetMinimumLevel(_logLevel).AddConsole().AddProvider(new LogFileProvider());
});

bool isService = ((args != null) && (args.Length > 0));
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && isService)
{
hostBuilder.UseContentRoot(AppContext.BaseDirectory);
hostBuilder.ConfigureLogging((hostingContext, logging) =>
{
logging.AddEventLog();
logging.SetMinimumLevel(_logLevel);
})
.ConfigureServices((hostContext, services) =>
{
services.AddSingleton<IHostLifetime, WindowsServiceLifetime>();
services.Configure<EventLogSettings>(settings =>
{
if (string.IsNullOrEmpty(settings.SourceName))
{
settings.SourceName = hostContext.HostingEnvironment.ApplicationName;
}
});
});
}

hostBuilder.Build().Run();
}
}

Expand Down

0 comments on commit 51c113e

Please sign in to comment.