question

SuryaKantNayak-3750 avatar image
0 Votes"
SuryaKantNayak-3750 asked sreejukg commented

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

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.

dotnet-csharpdotnet-aspnet-core-generaldotnet-aspnet-core-webapiazure-webapps-webjobsdotnet-aspnet-core-security
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SuryaKantNayak-3750, I have added additional tags to receive insights from the targetted audience/SMEs on the approaches.

0 Votes 0 ·
sreejukg avatar image
0 Votes"
sreejukg answered sreejukg commented

Hi @SuryaKantNayak-3750

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.

e.g.

 protected override async Task ExecuteAsync(CancellationToken stoppingToken)
 {
     while (!stoppingToken.IsCancellationRequested)
     {
         // First check whether there is a job
         if(JobExists())
         {
             // 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));
     }
 }




· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Sreejukg,

I got your point what you are saying.
You are saying that once our application start running with that our web job run infinite loop and from the UI suppose user click on Export All button then you are saying will store that instance to database and it will pick by web job and process it.
But here my doubt is that's correct web job will process it but the timing of web job to process and API response on Export All button click can't provide the result same time.
That means web job will do it's task but how will return the stream object to UI.

Our main goal is to once the web job completed its work then will return stream object to UI and download the file.

How to achieve this, please help.

Thanks for your support.

0 Votes 0 ·
sreejukg avatar image sreejukg SuryaKantNayak-3750 ·

If it is a long running task, basically it is not a best practice to make the user wait. If you want the direct response to user, you can not use background task.

Ideally, when the processing is done, you can generate a custom link to the exported file, and send a notification to the user with the exported file, once the background service is completed. The notification also can be triggered from background service.

1 Vote 1 ·
sreejukg avatar image
0 Votes"
sreejukg answered SuryaKantNayak-3750 commented

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

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for quick response.

One doubt if I create class which implement BackgroundService in the same API project then what about the web job process timing and where it will be run ?

Background - What actually I'm doing is that I'm trying to implement web job to run on demand basic.
For example I have a button in web page called as Export All, once user click the button then I'm hitting the Export API from their I have written all logic for render process and once the render process completed then API return the File stream object to web and file downloaded in browser.

Problem Statement - In this approach the problem was when I have large amount of data then my API call was time out, so I thought to implement a web job separably and call that web job from API method and from the web job method will call the render logic.

So now if I'm going using as you said approach that in the same project will implement background process then is it solve my problem and weather it will be good choice to do so or any drawback ?

All my code deployed on azure app service and thinking to create web job from azure portal and deploy my console app in that.

I'm now in the problem to decide which approach to choose and which will be solve my problem.

Please suggest, any thought would be help.

Thanks for your help.

0 Votes 0 ·