Cannot find compilation library 'System.Security.Cryptography.Pkcs' - FIX How to fix the InvalidOperationException: Cannot find compilation library location for package 'System.Security.Cryptography.Pkcs' Visual Studio 2022 error

Visual Studio 2017 - Lost Razor Intellisense and Syntax Highlight - How to Fix it

If you've stumbled upon this post, there's a high chance you're experiencing a rather odd Visual Studio 2022 issue which I had to deal with today.

I was working on a ASP.NET Web Application project that was working just fine - until I tried to change the debug Web Server from IISExpress to IIS. As you probably already know, doing that requires to apply the following changes:

  • Install IIS (you bet!) and manually create the Virtual Application for my project using IIS Manager.
  • Install the IIS Development-time support for Visual Studio, as explained here.
  • Relocate the secrets.json file, assuming you are using the Visual Studio User Secrets feature (read this post for more info on that), as IIS doesn't have the required permissions to read the default folder where Visual Studio creates the secrets.json file.
  • Adding a new IIS profile to the launchSettings.json file with the required settings (applicationUrl, sslPort, etc).

I was already aware of all that, hence I did the above steps without issues.

What I didn't expect was the following error, which occurred right after I launched the project in Debug mode:

InvalidOperationException: Cannot find compilation library location for package 'System.Security.Cryptography.Pkcs'

I struggled a bit on StackOverflow trying to figure out what happened, which helped me to understand that the issue was somewhat related to the Razor Runtime Compilation feature - which works perfectly on IISExpress, but seems to have some odd incompatibilities with IIS.

Unfortunately, the only workaround I found there was to disable that feature - which I really didn't want to since I love it.


Luckily I found a better fix: I just had to install the System.Security.Cryptography.Pkcs NuGet package, which immediately solved the issue. Yeah, it was just that simple!

This allowed me to keep the Razor Runtime Compilation feature enabled: yay!


I hope this helps other people having this same issue. For further info about this topic, read this StackOverflow thread.


About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

Leave a Reply

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

The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.