【问题标题】:System.AggregateException: 'Some services are not able to be constructed' In my ASP.net coreSystem.AggregateException:“无法构建某些服务”在我的 ASP.net 核心中
【发布时间】:2020-11-19 04:15:40
【问题描述】:

我有一个模型:

public class Checkout
{
    public string CheckoutId { get; set; }

    public List<CheckoutItem> CheckoutItems { get; set; }

}

我正在尝试在尊重 POCO 的同时向对象添加方法。所以我添加了一个存储库:

    public class CheckoutRepository : ICheckoutRepository
    {
        private readonly AppDbContext _appDbContext;
        private readonly Checkout _checkout;

        public CheckoutRepository(AppDbContext appDbContext, Checkout checkout)
        {
            _appDbContext = appDbContext;
            _checkout = checkout;

        }

        public void AddItem(unitItem item, int amount)
        {
              //Removed for brevity 
        }

        public void ClearCheckout()
        {
           //Details removed for brevity
        }

        public Checkout GetCart(IServiceProvider serviceProvider)
        {   
          //Details removed for brevity
        }

        public List<CheckoutItem> GetCheckoutItems()
        {
            //Details removed for brevity
        }

        public decimal GetCheckoutTotal()
        {
           //Details removed for brevity
        }

        public decimal RemoveItem(unitItem item)
        {
           //Details removed for brevity
        }

还有一个到存储库的接口

public interface ICheckoutRepository
    {
         Checkout GetCart(IServiceProvider serviceProvider);

        void AddItem(unitItem item, int amount);

        decimal RemoveItem(unitItem item);

        List<CheckoutItem> GetCheckoutItems();

        void ClearCheckout();

        decimal GetCheckoutTotal();
    }

当然,我将它添加到启动文件中。

services.AddTransient&lt;ICheckoutRepository, CheckoutRepository&gt;();

但是当我运行应用程序时出现错误

System.AggregateException: '有些服务不能 构造'

还有 2 个内部异常

1:

InvalidOperationException:验证服务时出错 描述符'ServiceType: BataCMS.Data.Interfaces.ICheckoutRepository 生命周期:瞬态实现类型: BataCMS.Data.Repositories.CheckoutRepository':无法解析 尝试使用“BataCMS.Data.Models.Checkout”类型的服务 激活“BataCMS.Data.Repositories.CheckoutRepository”。

还有 2:

InvalidOperationException:无法解析服务类型 尝试激活时出现“BataCMS.Data.Models.Checkout” 'BataCMS.Data.Repositories.CheckoutRepository'

真的可以对这个问题有所了解。

【问题讨论】:

  • 你在构造函数中传递了Checkout checkout,所以你需要从那里删除它或者在DI容器中注册它。

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


【解决方案1】:

当您查看 CheckoutRepository 构造函数时,您会看到您正在注入 Checkout 类的实例。 ASP.NET 不知道在哪里搜索要注入的该类的实例,因此您必须在 DI 容器中注册它。

将此添加到您的启动文件中:

services.AddTransient<Checkout>(new Checkout());

这是一种有点不同的注册类型。您依赖于Checkout 类的具体实现,而不是依赖于抽象。我已经向上面的示例传递了一个默认的无参数构造函数,但是您可以将任何其他构造函数传递给它,或者(取决于抽象)只需创建 ICheckout 接口并像注册 ICheckoutRepository 一样注册:

services.AddTransient<ICheckout, Checkout>();

更多关于 DI 的信息请见here

我也在this video中探索了实用的方法

【讨论】:

  • 非常感谢您的解决方案和解释,只是为了澄清 DI,注入具体类是不好的做法。我是否必须使用ICheckout 接口进行一些级别的抽象。但除此之外,该解决方案工作正常。再次感谢。
  • 嘿,保罗。没问题。这样想:有一天,您决定添加另一种结帐方法,但您的ICheckoutRepository 只接受一种结帐类型(具体的Checkout 类)和任何类型ICheckoutRepository 的内部工作原理(可以很多,因为它是一个接口)依赖于那个类,所以添加另一个可能会变得很痛苦。如果您引入ICheckout 接口,您将能够使用ICheckout 的任何实现来构造该repo。在测试时进行模拟时,它也会派上用场。阅读依赖倒置原则:)
【解决方案2】:

也许你错过了?

services.AddTransient<ICheckout, Checkout>();

希望我能帮上忙。

【讨论】:

    【解决方案3】:

    这是因为我在 IRepo 中声明为 IEnumerable 并从 repo 传递了一个列表。

    解决这个问题已经解决了我的问题

    【讨论】:

      猜你喜欢
      • 2021-12-12
      • 1970-01-01
      • 2022-08-20
      • 2021-02-27
      • 1970-01-01
      • 2021-07-07
      • 1970-01-01
      • 2021-03-14
      • 1970-01-01
      相关资源
      最近更新 更多