Last active
December 13, 2019 09:41
-
-
Save akilab/82ba0404c96789db400cac068fa7d798 to your computer and use it in GitHub Desktop.
dotnet coreでのWindowsサービスの開発(作成途中)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .NET Core 3.1でのWindowsサービスの作成方法を調べたので記録しておく。 | |
| .NET Coreでは、WorkerServiceというものを使うらしい | |
| 詳しくはまだわからない。 | |
| Riderでは、[Worker Service]とテンプレートが用意されている。 | |
| 基本的にはこれを使えばよいと思う。 | |
| NuGetで、[Microsoft.Extensions.Hosting.WindowsServices]をインストールする必要がある。 | |
| これを入れておかないと、UseWindowsServices()メソッドが呼び出せない。 | |
| dotnet cliで作成する場合。 | |
| https://codeburst.io/create-a-windows-service-app-in-net-core-3-0-5ecb29fb5ad0 | |
| >dotnet new sln -o WindowsServiceDemo -n Demo | |
| >cd .\WindowsServiceDemo\ | |
| >dotnet new worker -o Demo | |
| >dotnet sln add .\Demo\ | |
| 作成して、publishしたら、適当なディレクトリに配置して | |
| scコマンドで登録する。 | |
| ここまで試せた。 | |
| ログがどこに出るとかはまだわからない。 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Threading.Tasks; | |
| using Microsoft.Extensions.DependencyInjection; | |
| using Microsoft.Extensions.Hosting; | |
| namespace DemoService | |
| { | |
| public class Program | |
| { | |
| public static void Main(string[] args) | |
| { | |
| CreateHostBuilder(args).Build().Run(); | |
| } | |
| public static IHostBuilder CreateHostBuilder(string[] args) => | |
| Host.CreateDefaultBuilder(args) | |
| .UseWindowsService() // ここを追加する | |
| .ConfigureServices((hostContext, services) => | |
| { | |
| services.AddHostedService<Worker>(); | |
| }); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @echo off | |
| rem サービスを登録する | |
| rem []で囲まれたところをプログラムに合わせて修正 | |
| sc create ["Demo Service"] binPath=[C:\demo\DemoService.exe] | |
| rem 削除する場合 | |
| rem sc delete ["Demo Service"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| dotnet publish -c Release -o <Output> -r [RID] --self-contained | |
| Windows RID | |
| ポータブル (.NET Core 2.0 以降のバージョン) | |
| win-x64 | |
| win-x86 | |
| win-arm | |
| win-arm64 | |
| Windows 10 / Windows Server 2016 | |
| win10-x64 | |
| win10-x86 | |
| win10-arm | |
| win10-arm64 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Threading; | |
| using System.Threading.Tasks; | |
| using Microsoft.Extensions.Hosting; | |
| using Microsoft.Extensions.Logging; | |
| namespace DemoService | |
| { | |
| public class Worker : BackgroundService | |
| { | |
| private readonly ILogger<Worker> _logger; | |
| public Worker(ILogger<Worker> logger) | |
| { | |
| _logger = logger; | |
| } | |
| protected override async Task ExecuteAsync(CancellationToken stoppingToken) | |
| { | |
| while (!stoppingToken.IsCancellationRequested) | |
| { | |
| _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); | |
| await Task.Delay(1000, stoppingToken); | |
| // ここにやりたい処理を入れる | |
| // 1秒毎になにか動く | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment