How to pass concrete types in a service class constructor in .NET Core 2.2

270 Views Asked by At

i'm getting below error

Unable to resolve service for type 'System.Action1[DatabaseConsumer.MyFooClass]' while attempting to activate 'DatabaseConsumer.DatabaseScheduler1[DatabaseConsumer.MyFooClass]'.

My Service class looks like below example, I need to have Action action, TType objTType, string cron in the constructor as well, also, FooService implements IHostedService

    public class DatabaseScheduler<TType> : FooService
    {
        protected override string CronSchedule { get; set; }
        private Action<TType> _action;
        TType _obj;

        public DatabaseScheduler(IServiceScopeFactory serviceScopeFactory, Action<TType> action, TType objTType, string cron) : base(serviceScopeFactory, cron)
        {
            _action = action;
            CronSchedule = cron;
            _obj = objTType;
        }

        public override Task ProcessInScopeService(IServiceProvider serviceProvider)
        {
            //Call the ProcessIntegrationMessages()
            _action.Invoke(_obj);
            return Task.CompletedTask;
        }
    }

In the Startup.cs added my service class to IServiceCollection services

 services.AddSingleton(typeof(Microsoft.Extensions.Hosting.IHostedService), typeof(DatabaseScheduler<MyFooClass>));
0

There are 0 best solutions below