How to test Azure Function "Could not load file or assembly"

Edmondo Silvestri 0 Reputation points
2023-05-18T14:17:09.62+00:00

I recently got the error: "The Function function is in error: Could not load file or assembly Microsoft.Extensions.Logging.Abstractions, Version=7.0.0.0 ..." in a solution with a function project and a library project; my csproj files looked (stripped down to the relevant part) like this:

Function.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.2.0" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\Library\Library.csproj" />
  </ItemGroup>
</Project>

Library.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
  </ItemGroup>
</Project>

The problem arose when I updated Microsoft.Extensions.Logging.Abstractions to version 7 (I was updating all the imported libraries to the latest version).

This problem was an easy fix, as it only required to revert the library update, but the point is that it arose at run time, after the function was deployed; neiter the compiler, nor my unit tests highlighted it.

I'd like to write a unit (or integration) test to protect my project against this kind of problem, but I didn't figure out a solution.

Any suggestion?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,911 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Ryan Hill 30,281 Reputation points Microsoft Employee Moderator
    2023-05-22T12:37:44.3566667+00:00

    Hey @Edmondo Silvestri

    The only way I can think of is including an integration test in your project. If you're not familiar with the subject, I would consider looking at Azure Functions - Part 2 - Unit and Integration Testing for a reference.

    Once you've established your integration (and or functional) tests, you can incorporate these into your CI/CD pipeline. I know I've personally ran into this issue myself with this assembly and I always end updating the version to resolve the issue.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.