Good day all,
I am trying to implement a parameter into the controllers constructor but I cannot wrap my head around how to accomplish this.
In my 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();
}
Above you can see me try to add a single of TestCaseManager to the services
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();
}
}
Above you can see that the DbContext and TestCaseManager is passed in. I am able to properly pass in the context but the application breaks when i try to pass in the manager.
If anyone could help shed some light on how to properly do this, that would be great.
And no, please don't suggest just adding the manager in the construct.
An unhandled exception occurred while processing the request. InvalidOperationException: Unable to resolve service for type 'NQAP.Common.Stores.TestCaseStore`1[NQAP.Common.Models.TestCaseModel]' while attempting to activate 'NQAP.Common.Managers.TestCaseManager'.
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, ISet callSiteChain, ParameterInfo[] parameters, bool throwIfCallSiteNotFound)
Above is the error I am giving and here is the TestCaseManager Class
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? Does it require it's own types to be injected too? What error message do you get?TestCaseStore<TestCaseModel> pStoreinTestCaseManagerconstructor.