I am working with Azure Functions and I am having a problem that involves the NuGet "Microsoft.Extensions.Logging.Abstractions". My solution has been running perfectly fine, but recently I added a dependency which depends on "Microsoft.Extensions.Logging.Abstractions 5.0.0". The project builds fine, but when I run it. the following error appears: "Could not load file or assembly 'Microsoft.Extensions.Logging.Abstractions, Version=5.0.0.0".
I found some information related to this on a Github issue. The suggestions that I found were:
- Downgrade "Microsoft.Extensions.Logging.Abstractions" to version 3.1.0. I can't do this, since I have a third-party dependency that references that same NuGet, but with version 5.0.0.
- Upgrade to .NET 5.0. I can't do this, since I need to use Durable Functions, and those are not yet supported in Azure Functions with .NET 5.0.
I also tried using multiple versions of that same NuGet, but I got the same error (I tried the approach that is mentioned in this link).
Is there any way of solving this problem?
UPDATE: I created a minimal example in which the issue can be reproduced. It is located here. The branch feature/WithLogging3 runs as expected, but the feature/WithLogging5 fails with the message "'The host has not yet started". If I enable the option to Break the Debugger with Common Language Runtime Exceptions, I get the error "Could not load file or assembly 'Microsoft.Extensions.Logging.Abstractions, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified".
It seems that the project only fails when there is an ILogger being injected into a function. For instance, this one (taken directly from Visual Studio 2019 Template for Azure Functions with .NET Core 3.1):
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
string responseMessage = string.IsNullOrEmpty(name)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, {name}. This HTTP triggered function executed successfully.";
return new OkObjectResult(responseMessage);
}
If I remove the injected ILogger (and the line in which the logger is used in the method), then the project runs fine. However, I need to use a logger.
I know that the example I provided does not include a Durable Function, but it reproduces the issue, and it will reproduce even if you add a Durable Function to the project, or if you replace the current function with a Durable Function that takes an injected ILogger.
By the way, if it changes anything, I am using Visual Studio 2019 (Enterprise).