「.NET Core Console」の版間の差分
ナビゲーションに移動
検索に移動
(ページの作成:「| .NET Core | ASP.NET Core | ==.NET Core Console==」) |
|||
| 1行目: | 1行目: | ||
| − | | [[.NET Core]] | [[ASP.NET Core]] | | + | | [[.NET Core]] | [[ASP.NET Core]] | [[C Sharp]] | |
==.NET Core Console== | ==.NET Core Console== | ||
| + | |||
| + | ==設定とDIを組み込んだテンプレート== | ||
| + | ===プロジェクト作成=== | ||
| + | <pre> | ||
| + | $ dotnet new console -o docweb_bat | ||
| + | $ dotnet new gitignore | ||
| + | </pre> | ||
| + | ===NuGet=== | ||
| + | ====Configuration==== | ||
| + | <pre> | ||
| + | dotnet add package Microsoft.Extensions.Configuration.Json --version 5.0.0 | ||
| + | </pre> | ||
| + | ====DI==== | ||
| + | <pre> | ||
| + | dotnet add package Microsoft.Extensions.DependencyInjection --version 5.0.1 | ||
| + | dotnet add package Microsoft.Extensions.Options --version 5.0.0 | ||
| + | dotnet add package Microsoft.Extensions.Configuration.Binder --version 5.0.0 | ||
| + | </pre> | ||
| + | ===実装=== | ||
| + | <pre> | ||
| + | using System; | ||
| + | using System.IO; | ||
| + | using docweb.Services; | ||
| + | using docWeb.Models; | ||
| + | using docweb_bat.Jobs; | ||
| + | using Microsoft.Extensions.Configuration; | ||
| + | using Microsoft.Extensions.DependencyInjection; | ||
| + | using Microsoft.Extensions.Options; | ||
| + | |||
| + | namespace docweb_bat | ||
| + | { | ||
| + | public class Program | ||
| + | { | ||
| + | static void Main(string[] args) | ||
| + | { | ||
| + | IServiceCollection services = new ServiceCollection(); | ||
| + | |||
| + | var configuration = GetConfiguration(); | ||
| + | |||
| + | // 設定を設定管理オブジェクトに読み込む | ||
| + | // require Microsoft.Extensions.Configuration.Binder | ||
| + | IDocumentWebDatabaseSettings dbSettings | ||
| + | = configuration.GetSection(nameof(DocumentWebDatabaseSettings)) | ||
| + | .Get<DocumentWebDatabaseSettings>(); | ||
| + | |||
| + | // DIによりオブジェクト関連を構築 | ||
| + | services.AddSingleton<IDocumentWebDatabaseSettings>(sp => dbSettings); | ||
| + | services.AddSingleton<DocumentWebService>(); | ||
| + | // JOBS | ||
| + | services.AddSingleton<GetCollection>(); | ||
| + | |||
| + | // 作業を行うオブジェクトをサービスから取り出し実行 | ||
| + | var job = services.BuildServiceProvider().GetService<GetCollection>(); | ||
| + | job.Run(); | ||
| + | } | ||
| + | |||
| + | // 設定のロード | ||
| + | private static IConfiguration GetConfiguration() { | ||
| + | |||
| + | // 環境変数(dotnet run)もしくは、launch.json(デバッガ起動時) にて上書きする設定を指定する | ||
| + | // export DECWEB_ENV=Development | ||
| + | // or launch.json configurations.env section | ||
| + | var environmentName = Environment.GetEnvironmentVariable("DOCWEB_ENV"); // 環境変数 | ||
| + | Console.WriteLine($"Environment:{environmentName}"); | ||
| + | |||
| + | IConfiguration configuration = new ConfigurationBuilder() | ||
| + | .SetBasePath(Directory.GetCurrentDirectory()) | ||
| + | .AddJsonFile("appsettings.json", true, true) | ||
| + | // 設定ファイルの一部に含める ex appsettings.Development.json | ||
| + | .AddJsonFile($"appsettings.{environmentName}.json", true, true) | ||
| + | .Build(); | ||
| + | |||
| + | return configuration; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </pre> | ||
2021年7月3日 (土) 06:19時点における版
| .NET Core | ASP.NET Core | C Sharp |
目次
.NET Core Console
設定とDIを組み込んだテンプレート
プロジェクト作成
$ dotnet new console -o docweb_bat $ dotnet new gitignore
NuGet
Configuration
dotnet add package Microsoft.Extensions.Configuration.Json --version 5.0.0
DI
dotnet add package Microsoft.Extensions.DependencyInjection --version 5.0.1 dotnet add package Microsoft.Extensions.Options --version 5.0.0 dotnet add package Microsoft.Extensions.Configuration.Binder --version 5.0.0
実装
using System;
using System.IO;
using docweb.Services;
using docWeb.Models;
using docweb_bat.Jobs;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
namespace docweb_bat
{
public class Program
{
static void Main(string[] args)
{
IServiceCollection services = new ServiceCollection();
var configuration = GetConfiguration();
// 設定を設定管理オブジェクトに読み込む
// require Microsoft.Extensions.Configuration.Binder
IDocumentWebDatabaseSettings dbSettings
= configuration.GetSection(nameof(DocumentWebDatabaseSettings))
.Get<DocumentWebDatabaseSettings>();
// DIによりオブジェクト関連を構築
services.AddSingleton<IDocumentWebDatabaseSettings>(sp => dbSettings);
services.AddSingleton<DocumentWebService>();
// JOBS
services.AddSingleton<GetCollection>();
// 作業を行うオブジェクトをサービスから取り出し実行
var job = services.BuildServiceProvider().GetService<GetCollection>();
job.Run();
}
// 設定のロード
private static IConfiguration GetConfiguration() {
// 環境変数(dotnet run)もしくは、launch.json(デバッガ起動時) にて上書きする設定を指定する
// export DECWEB_ENV=Development
// or launch.json configurations.env section
var environmentName = Environment.GetEnvironmentVariable("DOCWEB_ENV"); // 環境変数
Console.WriteLine($"Environment:{environmentName}");
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", true, true)
// 設定ファイルの一部に含める ex appsettings.Development.json
.AddJsonFile($"appsettings.{environmentName}.json", true, true)
.Build();
return configuration;
}
}
}
© 2006 矢木浩人