-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
68 lines (58 loc) · 2.56 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
64
65
66
67
68
using System;
using System.IO;
using System.Threading.Tasks;
using Common;
using Hangfire.ConsoleHost.Sample.Common.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Hangfire.ConsoleHost.Sample.Server
{
class Program
{
static async Task Main(string[] args)
{
IHostBuilder builder = new HostBuilder()
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.SetBasePath(Directory.GetCurrentDirectory());
config.AddJsonFile("appsettings.json", optional: false);
config.AddJsonFile($"appsettings.{Environment.MachineName}.json", optional: true);
})
.ConfigureServices((hostingContext, services) =>
{
services.AddOptions();
services.Configure<HangfireOptions>(c => hostingContext.Configuration.GetSection("Hangfire").Bind(c));
// Hangfire Setup
services.AddTransient<BackgroundJobServerOptions>(_ =>
{
IOptions<HangfireOptions> options = _.GetRequiredService<IOptions<HangfireOptions>>();
HangfireOptions hangfireOptions = options.Value;
return new BackgroundJobServerOptions
{
Queues = hangfireOptions.QueueNames,
WorkerCount = hangfireOptions.WorkerCount,
ServerName = hangfireOptions.ServerName,
};
});
services.AddHangfire(a =>
a.UseSqlServerStorage(hostingContext.Configuration.GetConnectionString("Hangfire")));
// Services
services.AddTransient<IOutputPipe, ConsoleOutputpipe>();
services.AddTransient<IDemoService, DemoService>();
// Register Hangfire Host
services.AddHangfireHost();
// Startup
services.AddHostedService<MyHangfireApp>();
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration);
logging.AddConsole();
});
await builder.RunConsoleAsync();
}
}
}