Rupesh
Rupesh

Reputation: 555

Azure function V3 could not load file or assembly Microsoft.Extensions.DependencyInjection.Abstractions, Version=5.0.0.0 with EF core 5.0-rc1

Case

We are creating azure function v3 with .netcore 3.1. Using EF core 5.0-rc1 and Depdency Injection

1) DependecyInjection

[assembly: FunctionsStartup(typeof(xxxxx.Startup))]
namespace xxxxx
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            var services = builder.Services;
            var configBuilder = new ConfigurationBuilder()
                .SetBasePath(Environment.CurrentDirectory)
                .AddJsonFile("local.settings.json", true, reloadOnChange: true)
                .AddEnvironmentVariables() ;
            ConfigureServices(services);
            ConfigureAppSettings(services, configBuilder.Build());
            ConfigureLogging(services, configBuilder.Build());
        }
    }
}

2) EF core 5.0 rc-1

https://devblogs.microsoft.com/dotnet/announcing-entity-framework-core-efcore-5-0-rc1/

Error

Could not load file or assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.

image

Packages

Following are the packages referenced

image

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <AzureFunctionsVersion>v3</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="4.1.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.0-rc.1.20451.14" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.7" />
    <PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" />
    <PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

Troubleshooting

commenting the following line in startup.cs (Dependency injection) solves the problem

[assembly: FunctionsStartup(typeof(xxxxx.Startup))]

Upvotes: 15

Views: 19307

Answers (5)

Nivedha Lakshmanan
Nivedha Lakshmanan

Reputation: 307

If you are using .NET core 3.1 or below that. Downgrade the NuGet packages of Microsoft.Extensions.DependencyInjection and Microsoft.Extensions.DependencyInjection.Abstractions to 3.x.x.

Upvotes: 7

Ste Pammenter
Ste Pammenter

Reputation: 3148

I switched my Azure function to an isolated process, which supports .NET 5.0. This only came about recently (March 2021). It looks like .NET 5.0 won't be supported in-process as far as I can tell (.NET 6.0 will be). There is a guide published here, I've listed the steps I took along with that for an existing project.

Steps

In .csproj, target .NET 5.0 (<TargetFramework>net5.0</TargetFramework>) and add <OutputType>Exe</OutputType>. Switch to the following packages:

  • Microsoft.Azure.Functions.Worker
  • Microsoft.Azure.Functions.Worker.SDK
  • Also replace Microsoft.Azure.WebJobs.Extensions.* with Microsoft.Azure.Functions.Worker.* ones.

In local.settings.json change runtime to: "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated".

Add Program.cs if you don't have one. Use the following:

    public static class Program
    {
        public static void Main()
        {
            var host = new HostBuilder()
                .ConfigureFunctionsWorkerDefaults()
                .ConfigureServices(services => {
                    // Add your services here...
                })
                .Build();

            host.Run();
        }
    }

In your triggers, you'll need to update [FunctionName("Example")] to [Function("Example")], and update your using statements to Microsoft.Azure.Functions.Worker ones.

There some differences between .NET Core 3.1 and .NET 5.0 support listed at the bottom of that article here which is worth reviewing before making changes! Dependency injection is supported out the box though in the "normal" sense. I think it might be possible to rework Program.cs to use your existing startup.

Debugging

To debug the program, you can run the function using the Azure Function CLI and attach a Visual Studio debugger.

  1. Open terminal in CSProj directory.
  2. Run func start --dotnet-isolated-debug.
  3. When the app runs, you need to look for the process ID, or PID. The line looks something like " Azure Functions .NET Worker (PID: ###)".
  4. In Visual Studio. Debug > Attach to Process..., search by the PID in the previous step (dotnet.exe) and Attach.

Further reading

The documentation seems a bit sporadic, I also found another guide on developing and publishing .NET 5.0 Azure functions here.

There is this issue which sheds some more light on what is going on.

Upvotes: 3

John
John

Reputation: 521

I have a .Net Standard 2.1 lib project with a service that is injected into my function app. In my solution the lib referenced:

Microsoft.Extensions.Options v5.0.0

After I changed the package version to 3.1.11, my function app ran successfully. In other words, if any project referenced by your function app has a reference to a .Net 5.0 package, it looks like you will get this exception if you are using DI.

Current versions in my function app:
Function app: .Net Core 3.1
Azure Functions Version: 3
Other libs: .Net Standard 2.1

I started with .Net 5.0 for the function app but apparently that is not supported for now:
.NET 5 support on Azure Functions

Hopefully this answer will be obsolete before too long but as of Jan 24, 2021 don't use .Net 5.0 projects or package references in your Azure function app solution/projects.

Upvotes: 0

Thiago Silva
Thiago Silva

Reputation: 21

It's not supported in Azure Function v3, yet. https://github.com/Azure/azure-functions-vs-build-sdk/issues/472

But, if you want to test it locally, I was able to run updating the DLL's in the Azure Function Core Tools directory: C:\Program Files\Microsoft\Azure Functions Core Tools

I needed to replace/update these Dll's to use EF5 with my functions: Dll's list

Microsoft.Extensions.DependencyInjection.Abstractions.dll
Microsoft.Extensions.Logging.Abstractions.dll
Microsoft.Extensions.Options.dll
Microsoft.Extensions.Primitives.dll

Upvotes: 2

Joey Cai
Joey Cai

Reputation: 20067

The Microsoft.Azure.Functions.Extensions depends on .net standard 2.0.

enter image description here

While the Entity Framework Core 5.0 RC1 will not run on .Net standard 2.0 platforms, it requires .net standard 2.1. So it could not find the Microsoft.Azure.Functions.Extensions.

enter image description here

For more details, you could refer to this article.

Upvotes: 6

Related Questions