SATHEESH  P
SATHEESH P

Reputation: 429

How to Inject IHostingEnvironment in My Own Class

I have below code to create a log file.

public class LoggingService
{
    private readonly IHostingEnvironment env;

    public LoggingService(IHostingEnvironment env)
    {
            this.env = env;
    }

    public void WriteLog(string strLog)
    {
            //Some code is Here
    }
}

Following Access the Class in Controller Function

LoggingService log =  new LoggingService();
log.WriteLog("Inside GetUserbyCredential function");

When I am trying to create an instance from a controller class to pass some value to the function. At the time I am getting the following error

There is no argument given that corresponds to the required formal parameter 'env' of 'LoggingService.LoggingService(IHostingEnvironment)'

How to create the instance for this.

Upvotes: 0

Views: 6685

Answers (2)

TanvirArjel
TanvirArjel

Reputation: 32109

Problem is in the following line:

LoggingService log =  new LoggingService();

When you are creating Instance of LoggingService class you are not passing the IHostingEnvironment to its constructor and that's why its getting null.

To overcome this problem try as follows:

public interface ILoggingService
{
   void WriteLog(string strLog);
}

public class LoggingService : ILoggingService
{

    private readonly IHostingEnvironment env;

    public LoggingService(IHostingEnvironment env)
    {
        this.env = env;
    }
    public void WriteLog(string strLog)
    {

        //Some code is Here
    }
}

Then in your controller:

public class YourController : Controller
{
     private readonly ILoggingService _loggingService;
     public YourController(ILoggingService loggingService)
     {
         _loggingService = loggingService;
     }

     public IActionResult YourMethod()
     {
        _loggingService.WriteLog("Inside GetUserbyCredential function");
     }
}

Finally in the ConfigureServices method of the Startup class as follows:

public void ConfigureServices(IServiceCollection services)
{
    services.AddScoped<ILoggingService, LoggingService>();
}

Upvotes: 3

Simply Ged
Simply Ged

Reputation: 8672

DependencyInjection in .NET Core requires you to register your classes and their dependencies so it can create/retrieve any dependencies before creating an instance of your class.

Based on your comments I am assuming you have not registered your LoggingService with the services collection and that you are trying to create an instance in your controller.

First thing, you need to register your LoggingService with the IServiceCollection in startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<LoggingService>();

    services.AddMvc();
}

This tells the DI service that when a class asks for a LoggingService in it's constructor, it has to create an instance of this class. Behind the scenes .NET Core has already registered the IHostingEnvironment interface with the DI service so we don't need to worry about that. When the DI service has to create an instance of our LoggingService it will automatically inject the IHostingEnvironment dependency.

Now we need to inject the class into our controller:

public class ValuesController : Controller
{
    private readonly LoggingService _loggingService;

    public ValuesController(LoggingService loggingService)
    {
        _loggingService = loggingService;
    }

    ...
}

When .NET Core creates an instance of our ValuesController it will see if it knows how to create an instance of LoggingService - which is does because we told it about our class in startup.cs.

One thing to remember about Dependency Injection you almost always NEVER use new to create a class. (There are exceptions to this but you should always try to let the DI service create the classes without you needed to use the new keyword).

I suggest you spend some time reading the documentation on Dependency Injection so you fully understand how dependency injection works in ASP.NET Core and the different scopes available.

Upvotes: 1

Related Questions