Search code examples
c#.netazureazure-functionsazure-functions-isolated

Getting "System.IO.FileNotFoundException: Could not load file or assembly 'Azure.Core, Version=1.38.0.0" in Azure Function App


Currently, I am running a .NET 8 Azure Function app in Azure. It's a dotnet-isolated function running on a Linux App Service plan.

I am getting this error when trying to run the HTTP trigger function:

Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'Azure.Core, Version=1.38.0.0, Culture=neutral, PublicKeyToken=92742159e12e44c8'. The system cannot find the file specified.

The solution consists of four projects: Application.csproj, Status.API.csproj, Domain.csproj, and Infrastructure.csproj.

Here is an example of Status.API.csproj packages

<ItemGroup>
  <FrameworkReference Include="Microsoft.AspNetCore.App" />
  <PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="7.0.0" />
  <PackageReference Include="Azure.Identity" Version="1.11.0" />
  <PackageReference Include="Microsoft.ApplicationInsights" Version="2.22.0" />
  <PackageReference Include="Microsoft.Azure.AppConfiguration.Functions.Worker" Version="7.2.0" />
  <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.21.0" />
  <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
  <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.0" />
  <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.0" />
  <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
  <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.2.0" />
  <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.20" />
  <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
  <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

I am using dotnet list package --include-transitive to try and identify what is looking for the Azure.Core 1.38 version and it tells me there is package using it but I have to manually check from there. for example Azure.Identity is using Azure.Core(>=1.38.0).

I have tried to downgrade the packages, thinking the functions are not using the lasted .NET 8 version.

I will down grade the NuGet packages version some more.

I have gone through the Functions "Diagnose and solve problems":

Function App Down or Reporting Errors image

EDIT:

Here is an example if the PropertyGroup:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <OutputType>Exe</OutputType>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <DockerFastModeProjectMountDirectory>/home/site/wwwroot</DockerFastModeProjectMountDirectory>
    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
  </PropertyGroup>

EDIT

The last thing I thought about was removing the unit tests, and with the tests removed, it worked. I suspect its the Testcontainers.MsSql package (<PackageReference Include="Testcontainers.MsSql" Version="3.9.0" />).

I am still investing its dependencies.

Context for the test library packages

  <ItemGroup>
    <PackageReference Include="coverlet.collector" Version="6.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.7" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
    <PackageReference Include="Moq" Version="4.20.70" />
    <PackageReference Include="Testcontainers.MsSql" Version="3.9.0" />
    <PackageReference Include="xunit" Version="2.5.3" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
  </ItemGroup>

ANSWER

@Pravallika put me on the right track, thank you very much, what was happening was for some reason was <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.7" /> was referanicng an older version of Azure.Identity, I then updated Azure.Identity to version 1.11.4, but then I was getting a new error:

Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.

I then had to update Microsoft.Bcl.AsyncInterfaces to the latest version, I'm not 100% sure yet why its referencing an older version when it seems it shouldn't, but its working.


Solution

  • The issue could be due to the vulnerability/incompatibility of the package versions installed.

    I have installed Azure.Identity 1.11.4 version to be compatible with Azure.Core version 1.38.0:

    enter image description here

    I have used the same packages in my project with the below versions and the function app worked as expected.

    .csproj:

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <AzureFunctionsVersion>v4</AzureFunctionsVersion>
        <OutputType>Exe</OutputType>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
      </PropertyGroup>
        <ItemGroup>
            <FrameworkReference Include="Microsoft.AspNetCore.App" />
            <PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="7.0.0" />
            <PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.3.1" />
            <PackageReference Include="Azure.Identity" Version="1.11.4" />
            <PackageReference Include="Microsoft.ApplicationInsights" Version="2.22.0" />
            <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.21.0" />
            <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
            <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.0" />
            <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.0" />
            <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
            <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.2.0" />
            <PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
            <PackageReference Include="Microsoft.Azure.AppConfiguration.Functions.Worker" Version="7.2.0" />
            <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.20" />
            <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
            <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
        </ItemGroup>
      <ItemGroup>
        <None Update="host.json">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
        <None Update="local.settings.json">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <CopyToPublishDirectory>Never</CopyToPublishDirectory>
        </None>
      </ItemGroup>
      <ItemGroup>
        <Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext" />
      </ItemGroup>
    </Project>
    

    Packages available in Test project:

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
        <IsPackable>false</IsPackable>
        <IsTestProject>true</IsTestProject>
      </PropertyGroup>
      <ItemGroup>
          <PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.3.1" />
          <PackageReference Include="coverlet.collector" Version="6.0.0" />
          <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.7" />
          <PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
          <PackageReference Include="Microsoft.Extensions.Configuration.AzureAppConfiguration" Version="7.3.0" />
          <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
          <PackageReference Include="Moq" Version="4.20.70" />
          <PackageReference Include="Testcontainers.MsSql" Version="3.9.0" />
          <PackageReference Include="xunit" Version="2.5.3" />
          <PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
      </ItemGroup>
      <ItemGroup>
        <ProjectReference Include="..\FunctionApp2\FunctionApp2.csproj" />
      </ItemGroup>
      <ItemGroup>
        <Using Include="Xunit" />
      </ItemGroup>
    </Project>
    
    • The function app worked without any issues:

    enter image description here