1

This question previously asked actually

  1. Using Autofac to inject a dependency into the Main entry point in a console app
  2. Correct use of Autofac in C# console application

Just want to call the non-static method from my main static method in the console app

So I followed the above articles, built in this way

namespace SampleConsoleApp
{
    [ExcludeFromCodeCoverage]
    public class Program
    {
        private ISampleService _oSampleService;
        
        private static IContainer CompositionRoot()
        {
            var builder = new ContainerBuilder();
            builder.RegisterType<SampleInitialize>();
            builder.RegisterType<SampleService>().As<ISampleService>();
            return builder.Build();
        }
        public static void Main(string[] args)
        {
            CompositionRoot().Resolve<SampleInitialize>().Run_Auto();
        }
    }
}

namespace SampleConsoleApp
{
    public class SampleInitialize
    {
  
        private ISampleService _oSampleService;

        public SampleInitialize(ISampleService oSampleService)
        {
            _oSampleService = oSampleService;
        }

        public void Run_Auto()
        {
           var _list = _oSampleService.GetList();
        }
    }
}

namespace SampleConsoleApp
{
    public class SampleService : ISampleService
    {
        public SampleService(IContext context)
            : base(context)
        {
            _context = context;

        }
        
        public List<String> GetList()
        {
            var _list = new List<String>();
            
            ....
         
            return _list;
        }
    }
    
}

namespace SampleConsoleApp
{
    public interface ISampleService
    {
        List<String> GetList();
    }   
}

but I'm getting an error once this launch as following

None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'SampleConsoleApp.SampleInitialize' can be invoked with the available services and parameters:\r\nCannot resolve parameter 'SampleConsoleApp.SampleService.ISampleService oSampleService' of constructor 'Void .ctor(SampleConsoleApp.ISampleService)'.

Kelum
  • 1,745
  • 5
  • 24
  • 45
  • 3
    The error seems to be pointing to the inability to create SampleService because IContext cannot be resolved. A class implementing IContext needs to be registered. – hocho Nov 19 '20 at 06:40
  • @hocho so you meant, this class `public class SampleDbContext : DbContext, IContext` ? can I know where it should register? – Kelum Nov 19 '20 at 07:54

1 Answers1

0

As mentioned from @hocho you need to register IContext.

    private static IContainer CompositionRoot()
    {
        var builder = new ContainerBuilder();
        builder.RegisterType<SampleInitialize>();
        builder.RegisterType<SampleService>().As<ISampleService>();
        builder.RegisterType<SampleDbContext>().As<IContext>();
        return builder.Build();
    }

Afterwards you need to register everything needed by SampleDbContext constructor as well.

Kalikus
  • 66
  • 4
  • after register In this context class still getting the same error :( – Kelum Nov 24 '20 at 07:12
  • I found the reason and way to achieve this, will share here once I composed an article, thanks for your support – Kelum Nov 24 '20 at 08:31