Azure function 5 and EF Core 5 Could not load file or assembly > Microsoft.Extensions.DependencyInjection.Abstractions

Viewed 338

System.IO.FileNotFoundException : 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.

I am getting above error when running the tests in azure devops pipeline but not when running it local. Everything works locally fine.

I believe the error happens when the base class try to initialize DB Context using EF Core 5 at line 15.

enter image description here

Code stack:

Azure function isolated enabled .Net 5

EF Core 5

Can someone please help me understand why this is happening? Below is the screenshot of the packages referenced in the test project.

enter image description here

1 Answers

There can be two reasons for this, AFAIK:

  • Make sure you have all Nuget packages compatible to each other in the porject. Check if Microsoft.Extensions.DependencyInjection.Abstractions has the version which is compatible to all other Nuget packages in the project. One way to find dependencies is in the Nuget Package Manager:

    enter image description here

  • Make sure these packages are included in the .csproj file of the project:

    <ItemGroup>
      <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.12" />
      <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.0.1" OutputItemType="Analyzer" />
      <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.0.0" />
      <PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.4" />
      <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.4">
        <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        <PrivateAssets>all</PrivateAssets>
      </PackageReference>
      <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.4" />
      <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.2" />
      <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />
      <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
      <PackageReference Include="System.Net.NameResolution" Version="4.3.0" />
    </ItemGroup>
    
Related