ワーカーサービス (.NET Generic Host) 開発例1. PickupWorkerService
Section titled “ワーカーサービス (.NET Generic Host) 開発例1. PickupWorkerService”モダンな .NET の Generic Host (汎用ホスト) で、
常駐稼働型プロセス PickupWorkerService を実際に作ってみます。
今回開発する PickupWorkerService の機能仕様
Section titled “ 今回開発する PickupWorkerService の機能仕様”対象のフォルダ内をずっと監視し続け、
*.txt(何らかのテキストファイル) を受け取ったら、result.csvファイルへと処理日時とファイル名を追記し、
受け取ったファイルを削除する。
※対象フォルダは、設定ファイルでカスタマイズ可能とする。
プログラム開発
Section titled “ プログラム開発”NuGet パッケージの追加
Section titled “ NuGet パッケージの追加”今回 ライブラリ として、以下の NuGet パッケージを追加しています。
Microsoft.Extensions.HostingKurukuru
使用ライブラリの、ライセンス表記
Kurukuru
MIT License
Copyright (c) 2017-present Mayuki Sawatari
Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.PGソースコード (C#)
Section titled “ PGソースコード (C#)”Program.cs
Section titled “ Program.cs”using PickupWorkerService;
var builder = Host.CreateApplicationBuilder(args);builder.Services.AddHostedService<PickupWorker>();
var host = builder.Build();host.Run();PickupWorker.cs
Section titled “ PickupWorker.cs”namespace PickupWorkerService;
/// <summary>常駐監視ワーカークラス</summary>/// <param name="config">アプリケーション設定</param>/// <param name="logger">ログ出力用ロガー</param>public class PickupWorker( IConfiguration config, ILogger<PickupWorker> logger) : BackgroundService{ protected override async Task ExecuteAsync(CancellationToken stoppingToken) { // 常駐監視するフォルダの絶対パス var baseDir = config.GetValue<string>("Directory:WorkingDirectoryPath") ?? AppContext.BaseDirectory; // 処理結果を出力するファイルの絶対パス var resultFilePath = $"{baseDir}{Path.DirectorySeparatorChar}result.csv"; // 常駐稼働中の表示メッセージ var processingMsg = $"{baseDir} フォルダ内の *.txt ファイルを、常駐監視しています...";
// 常駐監視処理を開始 await Kurukuru.Spinner.StartAsync(processingMsg, async () => { while (!stoppingToken.IsCancellationRequested) { // 常駐監視しているフォルダ内の *.txt ファイルを処理する PickUpProcess();
// 1秒間待機 await Task.Delay(1000, stoppingToken); } });
/// <summary>常駐監視しているフォルダ内の *.txt ファイルを処理するローカル関数</summary> void PickUpProcess() => Directory .EnumerateFiles(baseDir, "*.txt") .ToList() .ForEach(x => { var fileName = Path.GetFileName(x); var fileNameWithoutExt = Path.GetFileNameWithoutExtension(x); var dateTime = $"{DateTime.Now:yyyy/MM/dd HH:mm:ss}"; var content = $"{dateTime}, {fileNameWithoutExt}";
// 処理結果を出力するファイルに、処理日時とファイル名を追記する File.AppendAllText( path: resultFilePath, contents: content + Environment.NewLine, encoding: System.Text.Encoding.UTF8 );
// ファイルを削除 File.Delete(x);
// ログ出力#pragma warning disable CA1873 logger.LogInformation("({d}) {f} ファイルを処理しました。", dateTime, fileName);#pragma warning restore CA1873 });
}}アプリ設定ファイル (JSON)
Section titled “ アプリ設定ファイル (JSON)”{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft.Hosting.Lifetime": "Information" }, "Console": { "FormatterName": "SingleLine", "FormatterOptions": { "SingleLine": true } } }, "Directory": { "WorkingDirectoryPath": "C:\\Temp\\PickupWorking" }}


