-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgramService.cs
More file actions
75 lines (64 loc) · 2.02 KB
/
ProgramService.cs
File metadata and controls
75 lines (64 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System.ComponentModel;
using System.Runtime.Versioning;
using System.ServiceProcess;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NServiceBus.Logging;
#region MsLoggingInService
using MsLogLevel = Microsoft.Extensions.Logging.LogLevel;
using MsLoggerFactory = Microsoft.Extensions.Logging.ILoggerFactory;
[DesignerCategory("Code")]
[SupportedOSPlatform("windows")]
class ProgramService :
ServiceBase
{
IEndpointInstance? endpointInstance;
MsLoggerFactory? loggerFactory;
static void Main()
{
using var service = new ProgramService();
if (ServiceHelper.IsService())
{
Run(service);
return;
}
service.OnStart(null);
Console.WriteLine("Bus started. Press any key to exit");
Console.ReadKey();
service.OnStop();
}
protected override void OnStart(string[]? args) =>
AsyncOnStart().GetAwaiter().GetResult();
async Task AsyncOnStart()
{
var services = new ServiceCollection();
services.AddLogging(builder =>
{
builder.AddFilter(level => level >= MsLogLevel.Information);
builder.AddConsole();
});
var provider = services.BuildServiceProvider();
loggerFactory = provider.GetRequiredService<MsLoggerFactory>();
var logFactory = LogManager.Use<MicrosoftLogFactory>();
logFactory.UseMsFactory(loggerFactory);
var endpointConfiguration = new EndpointConfiguration("EndpointName");
endpointConfiguration.EnableInstallers();
endpointInstance = await Endpoint.Start(endpointConfiguration);
}
protected override void OnStop() =>
AsyncOnStop().GetAwaiter().GetResult();
async Task AsyncOnStop()
{
if (endpointInstance != null)
{
await endpointInstance.Stop();
}
loggerFactory?.Dispose();
}
}
#endregion
class ServiceHelper
{
public static bool IsService() =>
throw new NotImplementedException();
}