uhaciogullari/HttpClientFactoryLite

Azure Functions V2 ASP.Net Core 2.1 - Could not load type 'Microsoft.Extentions.Http.HttpClientFactory' from assembly

Closed this issue · 6 comments

Describe the bug

I have an Azure Functions V2 - ASP.Net Core 2.1 project that creates a class in a .Net Standard project which uses HttpClientFactoryLite to create a new HttpClientFactory and it throws the following exception:

Could not load type 'Microsoft.Extensions.Http.HttpClientFactory' from assembly 'Microsoft.Extensions.Http, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.

To Reproduce

Steps to reproduce the behavior:

  1. Create an Azure Functions V2 (ASP.Net Core 2.1) project
  2. Create a .Net Standard Class Library and add HttpClientFactoryLite NuGet.
  3. Create a basic Class in the Class Library and create a HttpClientFactory in the constructor
using Microsoft.Extensions.Http;
using System;

namespace ClassLibrary1
{
    public class Class1
    {
        public Class1()
        {
            var httpClientFactory = new HttpClientFactory();
        }
    }
}
  1. Create a simple timer function in the Functions project
using System;
using ClassLibrary1;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;

namespace FunctionApp1
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static void Run([TimerTrigger("*/15 * * * * *")]TimerInfo myTimer, ILogger log)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

            try
            {
                var class1 = new Class1();
            }
            catch(Exception ex)
            {
                throw;
            }
        }
    }
}

  1. Run this and when stepping over the var class1 = new Class1(); line it will throw the following exception Could not load type 'Microsoft.Extensions.Http.HttpClientFactory' from assembly 'Microsoft.Extensions.Http, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.

Suspected Cause

I suspect this is because HttpClientFactoryLite uses the same assembly name and namespaces as the actual Microsoft.Extensions.Http NuGet package that the Azure Functions V2 is using. That being version 2.2.0.0 and HttpClientFactoryLite DLL being 0.2.0.

Sample Solution that replicates the issue.

HttpClientFactoryLiteAzureFunctionIssue.zip

Hi @RedBlack

I'm sorry I noticed this issue just now. I think the problem is you need to support .NET Core 2.2 at least. Could you try that in case you still want to resolve it?

Hi @uhaciogullari

No worries, i have tried the suggested but it did not fix the issue. We have worked around this ourselves by creating our own HttpClientFactory implementation.

Was it the namespace issue like you mentioned?

Yes, the namespace was the issue. We also implemented the Microsoft IHttpClientFactory interface rather then our own separate one. This did mean we lost the .Register() method that you had added but it was not a big issue for us.

Hello @RedBlack and @uhaciogullari

We have this exact problem! Is there a fix for this without creating our own HttpClientFactory implementation?

Thank you!

I changed the root namespace. Pull the latest version and add this using statement.

using HttpClientFactoryLite;

Cheers