This is one of the many issues that randomly occurs when you don’t expect it. Clearly something has changed but it’s rarely clear what. In my case this happened on a web service that had been running fine for a few weeks.
It was of those it works on my machine scenarios, which is a little frustrating as there were no patches scheduled on the server between now and when it was last published.
Fortunately the solution for me was simple I just added the following line to my main csproj file
This works for me as the web service is .Net Core 3.1 (LTS) and I have the dotnet hosting bundle installed on my server.
I then published to the second instance of the same web service as it sits behind a farm, and the second instance got the same error as above despite the identical code on the first instance working fine!
After a little checking of the app pool settings I realised that for both app pools the Load User Profile setting had been incorrectly turned to False. There is no reason I would ever have done that. Setting the value to false means that it uses IIS 6.0 behaviour which is not what I want, particularly for a .net core app. I have no idea when or how this happened, particularly as one instance worked and the other didn’t. For both app pools I turned Load User Profile to True (the default value) and they both worked (for now!!).
If this doesn’t solve your issue there are some suggestions on StackOverflow