0

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) { }
    }
6
  • 1
    For clarity, what exactly is the error you get when you pass in the manager? Commented Apr 19, 2018 at 16:03
  • So what's the error? Commented Apr 19, 2018 at 16:03
  • What is TestCaseManager? Does it require it's own types to be injected too? What error message do you get? Commented Apr 19, 2018 at 16:04
  • Are you trying to roll your own dependency-injection? Commented Apr 19, 2018 at 16:05
  • 2
    The error is absolutely self-descriptive, DI can't resolve TestCaseStore<TestCaseModel> pStore in TestCaseManager constructor. Commented Apr 19, 2018 at 16:08

1 Answer 1

2

To construct a service with type TestCaseManager, it needs to pass in a TestCaseStore<TestCaseModel> to its constructor. You are missing that from your services. For example:

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

Note, if the TestCaseStore requires and services itself, they will also need to be registered.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, I am still confused as I am still getting an error. I have update the question to show what the store class looks like.
Your store is expecting a DbContext, you need to tell the container about that, probably using the ApplicationDbContext you have.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.