Using Autofac to inject a dependency into the Main entry point in a console app

Kelum
1 min readNov 24, 2020

#CSharp #Log4Net #Autofac #ConsoleApplication #DependancyInjection

Here I’m going to share the simplest way we can use Autofac to inject the log4net log instance and ensure that the Log property will not be null at run time. The main issue is that we can’t pass it in through Main() Because Main is a static method and the service class method is a nonstatic method.

if someone supposes to inject these log methods inside Job schedulers this may help in those scenarios

So first as usual we have to define the Init class like following

SampleInitialize.cs

Run() method where its call the actual log4net method,

So in the main method, we have to register SampleInitialize class and Log class like the following.

Program.cs

So now can inject the Run method without creating a new instance of SampleInitialize class.

In my next article, I’ll share how to inject the service layer method(with entity framework) Using Autofac to inject a dependency into the Main entry point in a console app

--

--