Unable to resolve service for type 'Microsoft.AspNetCore.Hosting.IHostingEnvironment' while attempting to activate.

SuryaKanta Nayak 116 Reputation points

Hi Experts,

I have developed a console application as my web job and also I have developed web API to generate excel, both web job and all the API are in same solution.
I have added reference of Service inside Console application and calling the API.
But here I'm getting issue that in my ExcelRender service class I'm using IHostEnvironment as parameter inside constructor, so when I'm calling that service from web job I'm getting error that,

"Unable to resolve service for type 'Microsoft.AspNetCore.Hosting.IHostingEnvironment' while attempting to activate the ExcelRender service"

If I remove that IHostEnvironment parameter from that service constructor then web job run success without any error.

Inside web job Program.cs file I have injected all my API service using ConfigServices method of HostBuilder.

I have search this error and tried every possible way but not able to resolve it.

So can anyone help me to understand what is the meaning of service not able to activate and how to resolve this ?

When I verified all thing I found that in my web job program.cs class I'm using two reference that Microsoft.AspNetCore.Hosting and Microsoft.Extension.Hosting
In this two dll's we have IHostingEnvironment, but I'm not using IHostingEnviroment any where in my web job, it's present inside my web API service.
So I think it will not create this issue, but not sure.

Please help here, any suggestion valuable for me.

Thanks in advance.

Azure App Services
Azure App Services
A feature of Azure App Service used to create and deploy scalable, mission-critical web apps.
4,327 questions
A set of technologies in the .NET Framework for building web applications and XML web services.
2,596 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
7,010 questions
A set of technologies in the .NET Framework for building web applications and XML web services.
1,206 questions
{count} votes

Accepted answer
  1. sreejukg 9,171 Reputation points

    Hi @SuryaKanta Nayak

    What you are trying to achieve is perfectly doable with background service. As you can use, the ExecuteAsync method of the background service, you will be using infinite loop to execute your logic.

    In the loop, you may add your business logic, for e.g. when the user request an export task, you may write the entry to a database and your background task will check the existance of such an entry and process it.


    protected override async Task ExecuteAsync(CancellationToken stoppingToken)  
        while (!stoppingToken.IsCancellationRequested)  
            // First check whether there is a job  
                // write code here to do the export task  
            // The wait time to check for the jobs... you may define it as per your needs  
           await Task.Delay(TimeSpan.FromMinutes(5));  

1 additional answer

Sort by: Most helpful
  1. sreejukg 9,171 Reputation points

    If you have ASP.Net project, you can use the same project/application to run background jobs.

    Refer: https://weblogs.asp.net/sreejukg/running-background-tasks-in-asp-net-applications

    Hope this helps