Resolving the “Could Not Load File or Assembly System.IdentityModel.Tokens.Jwt” Error

Published by Mika Berglund on

Swagger UI

I was working on an Azure Functions application to create a REST API that accepts bearer tokens. When I fired up the app in Visual Studio, I was greeted with the following error message.

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.

This article describes the steps I took to resolve the issue for my Azure Functions application.

Could Not Load File or Assembly – Fix #1

The first fix was quite easy to find on Google. There are many posts that talk about a problem in the Azure Functions SDK. It seems that the SDK is too eager to clean out referenced assemblies that it thinks are not used. So, you need to instruct the Functions SDK to skip cleaning out by adding the following property group to the Azure Functions app project file.

<PropertyGroup>
    <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
</PropertyGroup>

That fixed the issue for me when running locally in debug mode in Visual Studio.

Could Not Load File or Assembly – Fix #2

That was an easy fix, I thought, so I deployed the application to my Azure App Service and fired off a request to one of the endpoints. However, I got the same error message as I did earlier.

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.

Oh snap! Using the App Service Editor, I verified that the System.IdentityModel.Tokens.Jwt.dll assembly really was not available in the bin folder. A lot of files, but unfortunately not the required. So apparently the _FunctionsSkipCleanOutput property did not fix everything.

I noticed that my Functions app did not contain a direct reference to System.IdentityModel.Tokens.Jwt but instead, my Functions app references another library, that had a reference to the Tokens assembly. So, I thought that I could easily fix the error by adding a direct reference to the missing package to my Functions app too.

But no. I was wrong! Still, the same error. I realized that if the Functions SDK incorrectly cleans out unused assemblies, it might not be enough just to add a reference to it. I would need to use it too. So I added the following code to the constructor of one of the function classes I had in my project.

using Microsoft.Extensions.Logging;
using System.IdentityModel.Tokens.Jwt;

public class ReportsFunctions
{
    public ReportFunctions(ILogger<ReportsFunctions> logger)
    {
        var token = new JwtSecurityToken();
    }
}

Now, when I deployed this project to Azure, it started working. I also verified that the System.IdentityModel.Tokens.Jwt.dll now was present in the bin folder.

Conclusion

After the second fix I thought that maybe I don’t need the first fix. The second fix would fix all my problems. That is however not true. Seems that you still need Fix #1, even after applying Fix #2. At least when running the app locally. And Fix #2 is not something that I found by Googling, so I hope this tip will come in handy, should you end up on this page one day.


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *