「.NET Core Console」の版間の差分
ナビゲーションに移動
検索に移動
(→実装) |
|||
| 29行目: | 29行目: | ||
</pre> | </pre> | ||
===実装=== | ===実装=== | ||
| − | * | + | *Progmram.cs |
<pre> | <pre> | ||
using System; | using System; | ||
| − | using System. | + | using System.Collections.Generic; |
| − | using | + | using System.Linq; |
| − | using | + | using System.Threading.Tasks; |
| − | using | + | using docweb_bat2.Models; |
| + | using docweb_bat2.Services; | ||
using Microsoft.Extensions.Configuration; | using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | using Microsoft.Extensions.DependencyInjection; | ||
| + | using Microsoft.Extensions.Hosting; | ||
| + | using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | using Microsoft.Extensions.Options; | ||
| − | namespace | + | namespace docweb_bat2 |
{ | { | ||
public class Program | public class Program | ||
{ | { | ||
| − | static void Main(string[] args) | + | public static void Main(string[] args) |
{ | { | ||
| − | + | CreateHostBuilder(args).Build().Run(); | |
| − | + | } | |
| − | |||
| − | + | public static IHostBuilder CreateHostBuilder(string[] args) => | |
| − | + | Host.CreateDefaultBuilder(args) | |
| − | + | .ConfigureLogging((hostContext, builder) => | |
| − | + | { | |
| − | . | + | builder.ClearProviders(); |
| − | + | builder.AddLog4Net(); | |
| − | + | }) | |
| − | + | .ConfigureServices((hostContext, services) => | |
| − | + | { | |
| − | + | // requires using Microsoft.Extensions.Options | |
| − | + | services.Configure<DocumentWebDatabaseSettings>( | |
| − | + | hostContext.Configuration.GetSection(nameof(DocumentWebDatabaseSettings))); | |
| − | + | services.AddSingleton<IDocumentWebDatabaseSettings>(sp => | |
| − | + | sp.GetRequiredService<IOptions<DocumentWebDatabaseSettings>>().Value); | |
| − | |||
| − | |||
| − | + | services.AddSingleton<IConfiguration>(hostContext.Configuration); | |
| − | + | services.AddSingleton<DocumentWebService>(); | |
| − | + | services.AddHostedService<Worker>(); | |
| − | + | }) | |
| − | + | ; | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
} | } | ||
} | } | ||
2021年7月4日 (日) 05:37時点における版
| .NET Core | ASP.NET Core | C Sharp |
目次
.NET Core Console
汎用HOST
設定とDIを組み込んだテンプレート
プロジェクト作成
$ dotnet new console -o docweb_bat $ dotnet new gitignore
NuGet
Configuration
dotnet add package Microsoft.Extensions.Configuration.Json --version 5.0.0 dotnet add package Microsoft.Extensions.Configuration.CommandLine --version 5.0.0
Logging
- Log4net
dotnet add package Microsoft.Extensions.Logging.Log4Net.AspNetCore --version 5.0.3
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
実装
- Progmram.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using docweb_bat2.Models;
using docweb_bat2.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace docweb_bat2
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging((hostContext, builder) =>
{
builder.ClearProviders();
builder.AddLog4Net();
})
.ConfigureServices((hostContext, services) =>
{
// requires using Microsoft.Extensions.Options
services.Configure<DocumentWebDatabaseSettings>(
hostContext.Configuration.GetSection(nameof(DocumentWebDatabaseSettings)));
services.AddSingleton<IDocumentWebDatabaseSettings>(sp =>
sp.GetRequiredService<IOptions<DocumentWebDatabaseSettings>>().Value);
services.AddSingleton<IConfiguration>(hostContext.Configuration);
services.AddSingleton<DocumentWebService>();
services.AddHostedService<Worker>();
})
;
}
}
- 設定ロード先
namespace docWeb.Models
{
public interface IDocumentWebDatabaseSettings
{
string DocumentWebCollectionName { get; set; }
string ConnectionString { get; set; }
string DatabaseName { get; set; }
}
public class DocumentWebDatabaseSettings : IDocumentWebDatabaseSettings
{
public string DocumentWebCollectionName { get; set; }
public string ConnectionString { get; set; }
public string DatabaseName { get; set; }
}
}
*サービス
namespace docweb.Services
{
public class DocumentWebService
{
// コンストラクタインジェクション
public DocumentWebService(IConfiguration configuration, IDocumentWebDatabaseSettings settings)
{
}
}
}
- JOB
namespace docweb_bat.Jobs
{
public class GetCollection
{
DocumentWebService _service;
public GetCollection(DocumentWebService service)
{
this._service = service;
}
public void Run()
{
}
}
}
© 2006 矢木浩人