Azure Function Could not load file or assembly Microsoft.IdentityModel.Protocols.OpenIdConnect

Oscar Ernesto Loyola Sánchez 1 Reputation point
2021-12-15T18:09:51.037+00:00

I implemented an application "Azure Functions" that makes use of "Microsoft.IdentityModel.Protocols.OpenIdConnect" to authenticate the session of a user, it compiles without errors, but starting at runtime throws the following error: "Could not load file or assembly 'Microsoft.IdentityModel.Protocols.OpenIdConnect ". I noticed that the error is resolved if I lower the version of "Microsoft.NET.Sdk.Functions" to "3.0.3", since from "3.0.4" it throws this error.

When creating the project, by default it uses version "3.0.13" of "Microsoft.NET.Sdk.Functions" (or the most current version). I am a little frustrated that I cannot use newer versions of "Azure Function" due to this limitation. If possible, could someone explain to me the reason for this error?

Thanks a lot.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,341 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,729 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Scott Rudy 26 Reputation points
    2021-12-17T19:42:32.32+00:00

    Seems Azure Functions does DLL cleanup to make the deployment smaller, since during deployment most of the files are already present, so you need to turn off that cleanup using <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput> in your project file, which will prevent the cleanup from running. There is another setting called FunctionsPreservedDependencies that is meant to allow you to opt-in DLLs that you want to preserve, but I couldn't get it to work for me.

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <AzureFunctionsVersion>v3</AzureFunctionsVersion>
        <!-- necessary to use > OpenIdConnect 6.10.2-->
        <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
      </PropertyGroup>
      <ItemGroup>
        <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1"/>
        <PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="6.15.0"/>
        <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.15.0"/>
      </ItemGroup>
    
      <ItemGroup>
        <None Update="host.json">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
        <None Update="local.settings.json">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <CopyToPublishDirectory>Never</CopyToPublishDirectory>
        </None>
      </ItemGroup>
    </Project>
    
    2 people found this answer helpful.
    0 comments No comments

  2. Bryan Knox 11 Reputation points
    2022-07-16T22:00:57.637+00:00

    _FunctionsSkipCleanOutput and FunctionsPreservedDependencies are still undocumented, so I wrote a blog post that may help fill the gap.

    https://bryanknox.github.io/2022/07/15/functionsskipcleanoutput-and-functionspreserveddependencies.html

    2 people found this answer helpful.
    0 comments No comments