Asp.net核心将ApplicationDBContext注入服务层

时间:2018-01-08 11:11:19

标签: dependency-injection asp.net-core

我是.Net核心新手,我试图将ApplicationDBContext注入服务层,但它不起作用。

以下是服务配置(ConfigureServices):

services.AddSingleton<IApplicationEventService, ApplicationEventService>();
services.AddScoped<ILoginHistoryService, LoginHistoryService>();
services.AddScoped<IApplicationEventHandler<UserLogedInEvent>, HandleUserLogedIn>();

LoginHistoryService的构造函数:

public LoginHistoryService(ApplicationDbContext context)
{
   _Context = context;
}

当用户登录时,AccountController提出应用程序事件:

await _eventService.Raise<UserLogedInEvent>(new UserLogedInEvent(user, DateTime.UtcNow));

但是当ApplicationEventService尝试解析所需的事件处理程序时会抛出一个excpetion:

var allInstance = _ServiceProvider.GetServices<IApplicationEventHandler<T>>();

,错误信息为:

  

无法解析&#39; System.Collections.Generic.IEnumerable`1 [App.Interfaces.IApplicationEventHandler`1 [App.Web.Infrastructure.Events.UserLogedInEvent]]&#39;   来自root提供程序,因为它需要作用域服务   &#39; App.Interfaces.IApplicationEventHandler`1 [App.Web.Infrastructure.Events.UserLogedInEvent]&#39;

请提供帮助和建议。

0 个答案:

没有答案
相关问题