【发布时间】:2017-07-29 11:43:58
【问题描述】:
这是一个 ASP.NET Core 默认项目ConfigureServices 方法:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}
我的问题是如何访问 ApplicationDbContext 中的电子邮件服务或短信服务?
或者假设我将构建一个自定义服务,并像这样在 DI 中注册它:
services.AddTransient<ICustomService, CustomService>();
我如何在其中访问电子邮件服务或短信服务?
我假设必须先将电子邮件和短信服务添加到 DI,然后才能使用它们,对吗?
【问题讨论】:
标签: c# dependency-injection asp.net-core