-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
78 lines (61 loc) · 2.48 KB
/
Program.cs
File metadata and controls
78 lines (61 loc) · 2.48 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
76
77
78
/*
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SapWorkerService;
using SapWorkerService.FileProcess;
using SapWorkerService.Services;
var builder = Host.CreateApplicationBuilder(args);
// Ãѹà»ç¹ Windows Service
builder.Services.AddWindowsService(o => o.ServiceName = "SAP Worker Service");
// Options
builder.Services.Configure<FileProcessorOptions>(
builder.Configuration.GetSection("FileProcessor"));
// DI: Infra + Services
builder.Services.AddSingleton<IDbConnectionFactory, SqlDbConnectionFactory>();
builder.Services.AddSingleton<IFileRouter, FileRouter>();
builder.Services.AddSingleton<IFileProcessor, ItemFileProcessor>();
builder.Services.AddSingleton<IFileProcessor, ForecastFileProcessor>();
builder.Services.AddSingleton<IFileProcessor, PurchaseOrderFileProcessor>();
builder.Services.AddHostedService<Worker>();
// Logging
builder.Logging.AddConsole();
builder.Logging.AddEventLog(s =>
{
s.SourceName = "SapWorkerService";
s.LogName = "Application";
});
var app = builder.Build();
app.Run();
*/
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using SapWorkerService.FileProcess;
using SapWorkerService.Services;
// --- àÃÔèÁµé¹ Host ---
var builder = Host.CreateApplicationBuilder(args);
// ---------- 1. Configure Logging ----------
builder.Logging.ClearProviders();
builder.Logging.AddConsole();
// ---------- 2. Configure Options (Inbound folder settings) ----------
builder.Services
.AddOptions<FileProcessorOptions>()
.Bind(builder.Configuration.GetSection("FileProcessorOptions"));
builder.Services
.AddOptions<ForecastProcessorOptions>()
.Bind(builder.Configuration.GetSection("ForecastProcessor"));
// ---------- 3. Register DI services ----------
// Database connection factory (Scoped)
builder.Services.AddScoped<IDbConnectionFactory, SqlDbConnectionFactory>();
// FileRouter + Processors (Scoped)
builder.Services.AddScoped<IFileRouter, FileRouter>();
builder.Services.AddScoped<IFileProcessor, ItemFileProcessor>();
builder.Services.AddScoped<IFileProcessor, ForecastFileProcessor>();
builder.Services.AddScoped<IFileProcessor, PurchaseOrderFileProcessor>(); // à¾ÔèÁµÑÇÍ×è¹ä´éµÒÁµéͧ¡ÒÃ
// Hosted Worker (Singleton, ãªé scope ÀÒÂã¹)
builder.Services.AddHostedService<Worker>();
// ---------- 4. Build & Run ----------
var host = builder.Build();
await host.RunAsync();