c# - ASP.NET Core Web App DI 错误 - 无法构造某些服务(验证服务描述符时出错

标签 c# asp.net-mvc asp.net-core

我正在创建一个 ASP.NET Core Web 应用程序。我正在通过库项目使用存储库。我在 web 应用程序项目中引用它。

仓库界面如下:

public interface IPushNotificationRepository
{
    IQueryable<PushNotification> Notifications
    {
        get;
    }
    IQueryable<Client> Clients
    {
        get;
    }

    void Add(PushNotification notification);
    void Add(Client client);
    void AddRange(IList<PushNotification> notifications);
    bool AddIfNotAlreadySent(PushNotification notification);
    void UpdateDelivery(PushNotification notification);
    bool CheckIfClientExists(string client);
    Client FindClient(int? id);
    void Update(Client client);
    void Delete(Client client);
}

在存储库中,我注入(inject)了数据库上下文
    public class PushNotificationRepository : IPushNotificationRepository
    {
        private readonly PushNotificationsContext _context;

        public PushNotificationRepository(PushNotificationsContext context)
        {
            _context = context;
        }
}

启动类的配置服务如下:
public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews();
    services.AddSingleton<IPushNotificationRepository, PushNotificationRepository>();
    services.AddDbContextPool<PushNotificationsContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("PushNotificationsConnection")));
}

在 Controller 类中,我使用存储库:
    public class ClientsController : Controller
    {
        //private readonly PushNotificationsContext _context;
        private readonly IPushNotificationRepository _pushNotificationRepository;

        public ClientsController(IPushNotificationRepository pushNotificationRepository)
        {
            _pushNotificationRepository = pushNotificationRepository;
        }
}

存储库类位于 Web 应用程序项目引用的单独库项目中。我收到的错误是:

System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Services.Messaging.Data.Abstract.IPushNotificationRepository Lifetime: Singleton ImplementationType: Services.Messaging.Data.PushNotificationRepository': Cannot consume scoped service 'Services.Messaging.Data.PushNotificationsContext' from singleton 'Services.Messaging.Data.Abstract.IPushNotificationRepository'.)'



非常感谢对此的一些建议

最佳答案

单例不能引用 Scoped 实例。错误信息很清楚。

Cannot consume scoped service 'Services.Messaging.Data.PushNotificationsContext' from singleton



PushNotificationsContext 被视为范围服务。您几乎不应该从单例中使用范围服务或 transient 服务。您还应该避免使用作用域服务中的 transient 服务。使用范围服务,注入(inject)你需要的东西是一个很好的做法,它会在请求后自动清理。

任何一个

services.AddTransient < IPushNotificationRepository, PushNotificationRepository>();



或者

services.AddScoped< IPushNotificationRepository, PushNotificationRepository>();



可以正常工作,但请检查您的设计。也许这不是您正在寻找的行为。

关于c# - ASP.NET Core Web App DI 错误 - 无法构造某些服务(验证服务描述符时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60322252/

相关文章:

c# - 如何在网络连接时以及用户登录时接收事件

javascript - jQuery、Asp.net -jAutoCalc : Implement auto calculation on the form to be appended in the table using jQuery

c# - 配置 ASP.NET Core 应用程序的正确顺序是什么?

c# - 成员(member)验证与接口(interface)

c# - C# : HTTP 407 error 中的代理基本身份验证

javascript - 如何在MVC中将整套模型附加到formdata并获取它

asp.net-mvc - ASP.NET MVC - int 和 DateTime 类型的模型验证

javascript - 引用错误: signalR is not defined

asp.net-core - 为什么 ContentType header 公开为不同的属性?

visual-studio-2015 - 在发布时应用 EF 迁移仅适用于某些 Web 项目