This question previously asked actually
- Using Autofac to inject a dependency into the Main entry point in a console app
- 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)'.