-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
63 lines (52 loc) · 1.93 KB
/
Program.cs
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
using NLog.Config;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Hosting.WindowsServices;
namespace l99.fanuc;
internal class Program
{
private static async Task Main(string[] args)
{
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
var builder = new HostApplicationBuilder();
if (WindowsServiceHelpers.IsWindowsService())
builder.Services.AddWindowsService(options =>
{
options.ServiceName = "fanuc-cnc-api";
});
builder.Services.AddSingleton(args);
builder.Services.AddHostedService<FanucService>();
IHost host = builder.Build();
await host.RunAsync();
}
}
public class FanucService : BackgroundService
{
private readonly IHostApplicationLifetime _lifetime;
private readonly string[] _args;
public FanucService(IHostApplicationLifetime lifetime, string[] args)
{
_lifetime = lifetime;
_args = args;
}
public override Task StartAsync(CancellationToken cancellationToken)
{
Console.WriteLine("Application starting...");
Console.WriteLine($"Bitness: {(IntPtr.Size == 8 ? "64-bit" : "32-bit")}");
LogManager.Configuration = new XmlLoggingConfiguration("nlog.config");
var config = new ConfigurationBuilder().Build();
LogManager.Setup().SetupExtensions(ext => ext.RegisterConfigSettings(config));
return base.StartAsync(cancellationToken);
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await new MyCode().RunAsync(stoppingToken);
}
public override Task StopAsync(CancellationToken cancellationToken)
{
Console.WriteLine("Application stopping...");
LogManager.Shutdown();
return base.StopAsync(cancellationToken);
}
}