【问题标题】:How to add parameter to controller constructor如何将参数添加到控制器构造函数
【发布时间】:2018-04-20 00:22:31
【问题描述】:

大家好,

我正在尝试在控制器构造函数中实现一个参数,但我无法理解如何完成此操作。

在我的 Startup.cs >

public void ConfigureServices(IServiceCollection services)
{    
    services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Framework.Configuration.GetConnectionString("ConnectionString")));
    services.AddSingleton<TestCaseManager>(); // This is what I think i have to do
    services.AddMvc();
}

上面你可以看到我尝试向服务中添加一个 TestCaseManager

public class TestCaseController : Controller
{
    // The scoped Application context
    protected ApplicationDbContext m_Context;    
    protected TestCaseManager m_TestCaseManager;

    public TestCaseController(ApplicationDbContext pContext, TestCaseManager pTestCaseManager)
    {
        m_Context = pContext;
        m_TestCaseManager = pTestCaseManager;

        m_Context.Database.EnsureCreated();
    }
}

您可以在上面看到传入了 DbContext 和 TestCaseManager。我能够正确地传入上下文,但是当我尝试传入管理器时应用程序中断。

如果有人可以帮助阐明如何正确执行此操作,那就太好了。

不,请不要建议只在构造中添加经理。

处理请求时发生未处理的异常。 InvalidOperationException:尝试激活“NQAP.Common.Managers.TestCaseManager”时无法解析“NQAP.Common.Stores.TestCaseStore`1[NQAP.Common.Models.TestCaseModel]”类型的服务。

Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, ISet callSiteChain, ParameterInfo[] 参数, bool throwIfCallSiteNotFound)

上面是我给出的错误,这里是 TestCaseManager 类

 public class TestCaseManager
        {
            protected internal TestCaseStore<TestCaseModel> m_store { get; set; }
            protected virtual CancellationToken m_cancellationToken => CancellationToken.None;

            public virtual IQueryable<TestCaseModel> TestCases
            {
                get
                {
                    var queryableStore = m_store as IQueryableTestCaseStore<TestCaseModel>;
                    if (queryableStore == null)
                        throw new NotSupportedException("StoreNotIQueryableUserStore");

                    return queryableStore.TestCases;
                }
            }

            public TestCaseManager(TestCaseStore pStore) => m_store = pStore;
        }


public class TestCaseStore : TestCaseStore<TestCaseModel>
    {
        public TestCaseStore(DbContext pContext) : base(pContext) { }
    }

    public class TestCaseStore<TTestCase> : TestCaseStore<TTestCase, DbContext, string>
        where TTestCase : TestCaseModel<string>
    {
        public TestCaseStore(DbContext pContext) : base(pContext) { }
    }

    public class TestCaseStore<TTestCase, TContext> : TestCaseStore<TTestCase, TContext, string>
        where TTestCase : TestCaseModel<string>
        where TContext : DbContext
    {
        public TestCaseStore(TContext pContext) : base(pContext) { }
    }

    public class TestCaseStore<TTestCase, TContext, TKey> : BaseDataStore<TTestCase, TContext, TKey>
        where TTestCase : TestCaseModel<TKey>
        where TContext : DbContext
        where TKey : IEquatable<TKey>
    {
        public TestCaseStore(TContext pContext) : base(pContext) { }
    }

【问题讨论】:

  • 为了清楚起见,当你传入管理器时,你得到的错误究竟是什么?
  • 那么错误是什么?
  • 什么是TestCaseManager?它是否也需要注入自己的类型?您收到什么错误消息?
  • 您是否尝试推出自己的依赖注入?
  • 错误绝对是自我描述的,DI 无法在TestCaseManager 构造函数中解析TestCaseStore&lt;TestCaseModel&gt; pStore

标签: c# asp.net-core


【解决方案1】:

要构造TestCaseManager 类型的服务,它需要将TestCaseStore&lt;TestCaseModel&gt; 传递给它的构造函数。您的服务中缺少这一点。例如:

services.AddSingleton<TestCaseStore<TestCaseModel>>();

注意,如果TestCaseStore 需要和服务本身,它们也需要注册。

【讨论】:

  • 对不起,我仍然感到困惑,因为我仍然收到错误消息。我已更新问题以显示商店类的外观。
  • 您的商店需要一个DbContext,您需要告诉容器这件事,可能使用您拥有的ApplicationDbContext
猜你喜欢
  • 2013-07-01
  • 2017-07-26
  • 2016-08-22
  • 1970-01-01
  • 2022-11-15
  • 2020-10-30
  • 1970-01-01
  • 2021-11-24
  • 2021-06-15
相关资源
最近更新 更多