【发布时间】:2016-11-21 17:28:50
【问题描述】:
一个基本的 ConfigureServices 方法如下所示:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
//custom logic here
}
就在方法结束之前,我想运行一些自定义逻辑。但是我需要访问 ApplicationDbContext 并从那里获取一些配置。另一件事是我想使用AuthMessageSenver 服务发送电子邮件。
我的问题:如何访问我在同一方法中声明的服务。或者如何使用我的 ApplicationdDbContext 从数据库中获取数据。
【问题讨论】:
-
我突然想到了......也许我使用自定义服务中间件并在那里运行我的逻辑?
-
你需要执行什么样的逻辑?是每次请求还是仅在应用程序启动时一次?
标签: c# entity-framework-core asp.net-core-1.0