asp.net-core – 在执行DI时指定服务选项的干净方法
发布时间:2020-09-21 21:48:08 所属栏目:asp.Net 来源:互联网
导读:所以我有一个服务,比方说,它是ASPNET Core上的电子邮件服务. 当我将我的服务添加到ASPNET DI容器时,我想在我的IServiceCollection上应用以下模式来设置我的服务. public interface IEmailService{ void SendMail(string recipient, string message);}public v
|
所以我有一个服务,比方说,它是ASPNET Core上的电子邮件服务. 当我将我的服务添加到ASPNET DI容器时,我想在我的IServiceCollection上应用以下模式来设置我的服务. public interface IEmailService
{
void SendMail(string recipient,string message);
}
public void ConfigureServices(IServiceCollection services)
{
//configures my service
services.AddEmailService<MyEmailService>(options => options.UseEmailServer(sender,smtpHost,smtpPort,smtpPassword));
}
我想知道如果可能的话,最好的方法是什么.我确信我需要为IServiceCollection上的.AddEmailService()方法创建扩展方法,但除此之外我不知道从哪里开始或看. 解决方法这是一个带注释的示例应用程序,可以让您了解不同的事情: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 = "firstname@lastname.com";
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("hello@recipient.com","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中提取一些信息,并从传入的参数中获取一些信息. 编辑:这些是必需的包: "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" (编辑:日照站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 适用于Linq To SQL DAL的静态方法吗?
- asp.net 大文件上传 之 改版了的SlickUpload.HttpUploadMod
- asp.net-mvc – 文件上传MVC
- asp.net-mvc-4 – WepApi控制器是否应该返回viewmodels
- ASP.NET Core 1.0 ConfigurationBuilder().AddJsonFile(“a
- asp.net-mvc – 如何锁定ASP.NET MVC中的路径?
- 什么是使用aspnet_compiler.exe预编译ASP.NET项目的优势?
- 加快ASP.NET中的构建时间
- ASP.NET 4的IIS和服务器操作系统要求
- asp.net – 为每个网站/应用程序创建单独的IIS应用程序池的
推荐文章
站长推荐
- asp.net-mvc – asp.net mvc如何正确测试控制器
- asp.net-mvc – 有什么技巧/技巧使用亚音速与Asp
- asp.net – 网站随时随地突破
- asp.net-mvc – MVC 5:Asp.net身份:如何建模Us
- asp.net-mvc – 在EditorFor for child对象中使用
- ASP.NET 4的IIS和服务器操作系统要求
- asp.net-mvc – 如何在asp.net mvc中处理分页?
- asp.net – 将Eval参数从ASPX文件传递给JavaScri
- asp.net – FF和IE不从CSS加载img src
- .net – 什么可以解释托管堆上超过5,000,000个Sy
热点阅读
