ここでは、さまざまなものが何をしているか知っているようにコメントしてアプリケーション例です:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
// Add the options stuff. This will allow you to inject IOptions<T>.
services.AddOptions();
// This will take care of adding and configuring the email service.
services.AddEmailService<MyEmailService>(options =>
{
options.Host = "some-host.com";
options.Port = 25;
options.Sender = "[email protected]";
options.Username = "email";
options.Password = "sup4r-secr3t!";
});
}
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
// Make sure we add the console logger.
loggerFactory.AddConsole();
app.Use(async (context, next) =>
{
// Retrieve the email service from the services.
var emailService = context.RequestServices.GetRequiredService<IEmailService>();
// Send the email
await emailService.SendMail("[email protected]", "Hello World!");
});
}
public static void Main(string[] args)
{
WebApplication.Run<Startup>(args);
}
}
public interface IEmailService
{
Task SendMail(string recipient, string message);
}
public class EmailOptions
{
public string Sender { get; set; }
public string Host { get; set; }
public int Port { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
public class MyEmailService : IEmailService
{
public MyEmailService(IOptions<EmailOptions> options, ILogger<MyEmailService> logger)
{
Options = options; // This contains the instance we configured.
Logger = logger;
}
private IOptions<EmailOptions> Options { get; }
private ILogger<MyEmailService> Logger { get; }
public Task SendMail(string recipient, string message)
{
// Send the email
var builder = new StringBuilder();
builder.AppendLine($"Host: {Options.Value.Host}");
builder.AppendLine($"Port: {Options.Value.Port}");
builder.AppendLine($"Username: {Options.Value.Username}");
builder.AppendLine($"Password: {Options.Value.Password}");
builder.AppendLine("---------------------");
builder.AppendLine($"From: {Options.Value.Sender}");
builder.AppendLine($"To: {recipient}");
builder.AppendLine("---------------------");
builder.AppendLine($"Message: {message}");
Logger.LogInformation(builder.ToString());
return Task.FromResult(0);
}
}
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddEmailService<TEmailService>(this IServiceCollection services, Action<EmailOptions> configure)
where TEmailService : class, IEmailService
{
// Configure the EmailOptions and register it in the service collection, as IOptions<EmailOptions>.
services.Configure(configure);
// Add the service itself to the collection.
return services.AddSingleton<IEmailService, TEmailService>();
}
}
そして、ここでは、コンソールで実行中のアプリケーションです:
あなたが見ることができるようにアプリケーションはコンフィグレーションされたEmailOptions
からいくつかの情報を引き出しており、渡された引数の情報がいくつか渡されます。
EDIT:これらは、必要なパッケージは以下のとおりです。
"Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
"Microsoft.Extensions.OptionsModel": "1.0.0-rc1-final",
"Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final"
同様の質問ここhttp://stackoverflow.com/questions/32599573/how-do-i-inject-asp-net-5-vnext-user - 私自身のユーティリティクラスの秘密/ 32608820#32608820 –