0

I am using creating a library that enables developer to add services at runtime. I am not sure if I'm doing this right but below is my code that I made for this:

Test Class

public class Test : ITest
{
    public void Run()
    {
        string testMessage = "Message!";
    }
}

Test Interface

public interface ITest
{
    void Run();
}

Abstract Base Class

public abstract class BaseClass
{
    private IContainer Container { get; }

    // I want to utilize this one to register new services
    // on top of the existing registered services
    public IServiceCollection RegisterService { get; set; }
    protected BaseClass()
    {
        Init();
        // Add new service via container
        Container.Configure(RegisterService);
    }

    void Init()
    {
         Container = new Container.For<ExistingRegistries>();
         RegisterService = new ServiceRegistry();
    }

    protected internal void ExecuteService<TAction>(Action<TAction> action)
    {
        action?.Invoke(Container.GetRequiredService<TAction>());
    }
}

Test Runner Console

public class ScheduledSales : BaseClass
{
    public ScheduledSales()
    {
        // Runtime registration of dependency
        RegisterService.AddTranscient<ITest, Test>();
    }

    public void Execute()
    {
        // Upon executing this one it will give me the error failed to register
        ExecuteService<AnotherButIgnoreThisService>(e => e.ExecuteNow());
    }
}

Error

Lamar.IoC.LamarMissingRegistrationException: 'No service registrations exist for xxxx or can be derived because: Cannot fill the dependencies of any of the public constructors Available constructors:new TestScheduleExtensions(IDataService dataService, ITest test) ITest is not registered within this container and cannot be auto discovered by any missing family policy '

What I am trying to achieve is to add and register the ITest and Test dependecies into the container and use it but somehow it didn't work.

I am not sure if I got the lamar documentation right but would be a great help if someone can pinpoint to me what I missed or done wrong with my code.

Alvin Quezon
  • 1,089
  • 3
  • 11
  • 29

0 Answers0