azure-functions-host
azure-functions-host copied to clipboard
Could not load file or assembly Microsoft.IdentityModel.Tokens (related to _FunctionsSkipCleanOutput)
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
- Clone my repo: https://github.com/srmagura/FunctionsAssemblyLoadingRepro
- Open the solution file in Visual Studio 2022.
- Run the project.
- 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
Thank you for your feedback! We will check for the possibilities internally and update you with the findings.
Similar: https://github.com/AzureAD/microsoft-identity-web/issues/1428
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.
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.
Hello @Ved2806, why did you mark this as "Needs: Author Feedback"? What feedback do you need from me? Thanks.
Hi @fabiocav, @brettsam Could you please look into this? We are able to repro this.
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?
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.
@Ved2806 Why did you mark this as "Needs: Author Feedback"? What feedback do you need from me?
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.
Commenting to keep issue open
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.
Sounds like there is a fix on the way being tracked in issue https://github.com/AzureAD/microsoft-identity-web/issues/1548
Hi @srmagura Please refer the conversation here #7592 and let us know if it helps? Thanks.
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.
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.
Not stale
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
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.
Same problem for me
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)
Same problem for me
![]()
Did you get a solution on this?
@Buyani For now, my only workaround is to downgrade the package
@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 No error with 6.10.2 from my side. Start to fail with 6.11.0
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
same issue here - the <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput> resolved this for now, when can i remove this configuration ?
encountering this issue also. Will also be doing as mentioned in 7601 and adding this to all future projects until this is fixed.
Hi @mattchenderson Could you please help with this issue?
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.