azure-functions-host icon indicating copy to clipboard operation
azure-functions-host copied to clipboard

Could not load file or assembly Microsoft.IdentityModel.Tokens (related to _FunctionsSkipCleanOutput)

Open srmagura opened this issue 3 years ago • 62 comments

A newly-created Azure Functions project fails to load Microsoft.IdentityModel.Tokens even when the .csproj file contains a PackageReference to the Microsoft.IdentityModel.Tokens NuGet package.

Investigative information

N/A, this issue is reproducible locally.

Repro steps

  1. Clone my repo: https://github.com/srmagura/FunctionsAssemblyLoadingRepro
  2. Open the solution file in Visual Studio 2022.
  3. Run the project.
  4. Go to http://localhost:7071/api/Function1 in your web browser.

Expected behavior

There should be no errors in the debug window. The browser should display the text "Function executed."

Actual behavior

An error message is displayed in the debug window:

System.Private.CoreLib: Exception while executing function: Function1. FunctionsAssemblyLoadingRepro: Could not load file or assembly 'Microsoft.IdentityModel.Tokens, Version=6.14.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.

Known workarounds

Add <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput> to the .csproj file. This is a workaround, not a solution, because this property is internal and undocumented.

Related issues

  • https://github.com/Azure/azure-functions-host/issues/7601
  • https://github.com/Azure/azure-functions-host/issues/5894
  • https://github.com/Azure/azure-functions-host/issues/7061
  • https://github.com/Azure/azure-functions-host/issues/5756

srmagura avatar Nov 22 '21 22:11 srmagura

Thank you for your feedback! We will check for the possibilities internally and update you with the findings.

Ved2806 avatar Nov 24 '21 15:11 Ved2806

Similar: https://github.com/AzureAD/microsoft-identity-web/issues/1428

Cogax avatar Nov 24 '21 16:11 Cogax

Hi.. try lowering your azure function version, for example:

<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netcoreapp3.1</TargetFramework> <AzureFunctionsVersion>v3</AzureFunctionsVersion> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="6.14.1" /> <PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.14.1" /> <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.3" /> </ItemGroup> <ItemGroup> <None Update="host.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </None> <None Update="local.settings.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToPublishDirectory>Never</CopyToPublishDirectory> </None> </ItemGroup> </Project>

Btw, I have a similar problem when updating to a higher version of Azure Function.

emriti avatar Nov 29 '21 05:11 emriti

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

ghost avatar Dec 06 '21 16:12 ghost

Hello @Ved2806, why did you mark this as "Needs: Author Feedback"? What feedback do you need from me? Thanks.

srmagura avatar Dec 06 '21 16:12 srmagura

Hi @fabiocav, @brettsam Could you please look into this? We are able to repro this.

Ved2806 avatar Dec 16 '21 17:12 Ved2806

Also ran into this trying to reference the latest Microsoft.IdentityModel.JsonWebTokens library at version 6.15.0 It can be repro'd by creating a new azure functions project in vs code using the azure functions extension. Select dotnet 6.0 (in-process) and runtime host of v4. If I try and reference any objects from that library I get the dreaded Could not load file or assembly error. I did quite the bit of research. Most roads point to the saga that is #5894, and the _FunctionsSkipCleanOutput suggestion there DID work.

However - as others have said - it is a bit of a club. The whole all or nothing approach to cleaning the output. I took a look at the FunctionsPreservedDependencies feature that was added in pull 6849 in order to address this defeciency, and it didn't work for me.

Lastly, I did notice that if I pin my Microsoft.IdentityModel.JsonWebTokens version to 6.10.0 everything worked just fine. 🎉

Full example Azure functions csproj below

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="6.10.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.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>
</Project>

Sharing this pin to 6.10.0 solution for other folks that don't may not want to use _FunctionsSkipCleanOutput or FunctionsPreservedDependencies. My guess is this works because the azure functions host is currently using this version? Can see it listed in this repo here. Sure would be nice to be able to reference the latest 6.15.0 Microsoft.IdentityModel.JsonWebTokens and it's security enhancements.

Burned a day researching this. Looks like @anthonychu has got feature request 6650 that might help folks save some research time on this?

TroyWitthoeft avatar Jan 03 '22 21:01 TroyWitthoeft

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

ghost avatar Mar 11 '22 16:03 ghost

@Ved2806 Why did you mark this as "Needs: Author Feedback"? What feedback do you need from me?

srmagura avatar Mar 11 '22 16:03 srmagura

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

ghost avatar Mar 15 '22 19:03 ghost

Commenting to keep issue open

srmagura avatar Mar 15 '22 19:03 srmagura

Same issue here - .net6, functions runtime v4. Any version above 6.10.2 fails to load. Given the upcoming EOL for .Net Core driving migrations to .Net 6, and the ever present need to remain up to date with security fixes in any app, being unable to keep a security-focused library up to date without workarounds while migrating to .Net6 is very frustrating.

david-eagles avatar Apr 30 '22 03:04 david-eagles

Sounds like there is a fix on the way being tracked in issue https://github.com/AzureAD/microsoft-identity-web/issues/1548

muzzar78 avatar May 10 '22 15:05 muzzar78

Hi @srmagura Please refer the conversation here #7592 and let us know if it helps? Thanks.

Ved2806 avatar Aug 25 '22 17:08 Ved2806

That issue is for Functions runtime v3 whereas this is for v4. This comment does explain what is going on, but AFAIK, it is never mentioned in the Azure Functions documentation that there are certain assemblies that you are not allowed to use. If this is a restriction imposed by the Functions runtime, it needs to be stated very clearly.

srmagura avatar Aug 25 '22 17:08 srmagura

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

ghost avatar Aug 29 '22 19:08 ghost

Not stale

srmagura avatar Aug 29 '22 19:08 srmagura

Got the same issue. "Could not load file or assembly 'Microsoft.IdentityModel.Tokens'" (version 6.16.0) even when the .csproj file contains a PackageReference.

.net6 / azure functions v4

mbaranovych avatar Sep 07 '22 12:09 mbaranovych

Love ho MS keeps marking tis item as stale, fact is, it is a bug and needs to be addressed. Ran into it today. So not stale.

TerrySansom avatar Sep 14 '22 21:09 TerrySansom

Same problem for me image image

foliv57 avatar Sep 21 '22 13:09 foliv57

Will this issue be resolved?Just ran into the same issue today: Error =>Could not load file or assembly 'System.IdentityModel.Tokens.Jwt, Version=6.23.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.

at System.Reflection.RuntimeAssembly.InternalLoad(ObjectHandleOnStack assemblyName, ObjectHandleOnStack requestingAssembly, StackCrawlMarkHandle stackMark, Boolean throwOnFileNotFound, ObjectHandleOnStack assemblyLoadContext, ObjectHandleOnStack retAssembly) at System.Reflection.RuntimeAssembly.InternalLoad(AssemblyName assemblyName, RuntimeAssembly requestingAssembly, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, AssemblyLoadContext assemblyLoadContext) at System.Reflection.Assembly.Load(AssemblyName assemblyRef) at Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Extensions.AssemblyExtensions.GetLoadableTypes(Assembly assembly) at Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Resolvers.OpenApiHttpTriggerAuthorizationResolver.Resolve(Assembly assembly) at Microsoft.Azure.WebJobs.Extensions.OpenApi.OpenApiHttpTriggerContext.get_OpenApiHttpTriggerAuthorization() at Microsoft.Azure.WebJobs.Extensions.OpenApi.OpenApiHttpTriggerContext.AuthorizeAsync(IHttpRequestDataObject req) at Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Extensions.OpenApiHttpTriggerContextExtensions.AuthorizeAsync(Task`1 context, IHttpRequestDataObject req) at Microsoft.Azure.WebJobs.Extensions.OpenApi.OpenApiTriggerFunctions.RenderSwaggerUI(OpenApiHttpTriggerContext openApiContext, HttpRequest req, ExecutionContext ctx, ILogger log)

Buyani avatar Oct 03 '22 10:10 Buyani

Same problem for me image image

Did you get a solution on this?

Buyani avatar Oct 03 '22 10:10 Buyani

@Buyani For now, my only workaround is to downgrade the package

foliv57 avatar Oct 03 '22 10:10 foliv57

@foliv57 You downgraded to which version?
I have the same issue with System.IdentityModel.Tokens.Jwt, Version=6.23.1.0, I downgraded but I still get "Could not load file or assembly..."

Valid workaround (mentionned above): <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>

patrick-lachance avatar Oct 03 '22 20:10 patrick-lachance

@patrick-lachance No error with 6.10.2 from my side. Start to fail with 6.11.0

foliv57 avatar Oct 04 '22 09:10 foliv57

Same issue here - and we've been to get around the error by going back to version 6.10.2 of the System.IdentityModel.Tokens.Jwt package without needing to use the _FunctionsSkipCleanOutput workaround

chris-parker-bliss avatar Oct 06 '22 09:10 chris-parker-bliss

same issue here - the <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput> resolved this for now, when can i remove this configuration ?

CISOMateAdmin avatar Oct 11 '22 07:10 CISOMateAdmin

encountering this issue also. Will also be doing as mentioned in 7601 and adding this to all future projects until this is fixed.

jblack5 avatar Oct 18 '22 18:10 jblack5

Hi @mattchenderson Could you please help with this issue?

Ved2806 avatar Oct 25 '22 13:10 Ved2806

With runtime 4.11.2.19273 and Microsoft.NET.Sdk.Functions 4.1.3, you should be able to successfully reference up to 6.21.0 of Microsoft.IdentityModel.Tokens. I was able to succeed with that using the originally provided repro project.

That version is still behind latest public, and if I did update the package past that, I do indeed see the error as described. 6.21.0 is from ~4 months ago, and the latest (6.24.0) is from about 12 days ago.

Short term tactics

We have opened a PR to snap to latest (6.24.0): https://github.com/Azure/azure-functions-host/pull/8863

You will need to update to the latest Functions dependencies once that is fully in and a release is cut.

Fixing this longer-term

Eventually a new version of Microsoft.IdentityModel.Tokens will come out, and the host will be behind again, causing this issue to present until yet another host version can be released which absorbs that update. As has been mentioned here, FunctionsSkipCleanOutput is a workaround, but it's not ideal.

We need to discuss the best way to fix this and will be doing so. One option on the table that @fabiocav and I were chatting about briefly today would be to add this to assembly unification. That means at runtime, your app would snap to whatever the host has. It would prevent this error from occurring and causing a crash, but it would also mean you that you aren't actually getting the latest version you specify. There would still be a dependency on the host to continue moving this forward. To be clear, we would intend to keep updating it (within the major version range), but there is a question of how much lag time is acceptable, etc. We will be looking at process improvements regardless.

Assembly unification comes with some tradeoffs, and it's not clear that's the direction we'd go, but I wanted to capture that based on the conversation. We'll continue discussing options - hopefully the tactical version update will help folks here for the time being while we do.

mattchenderson avatar Oct 25 '22 22:10 mattchenderson