Dude, Where's My Framework?

You start out your day with a nice cup of coffee, and think, "Ah, greenfield project day...smooth sailing".  You fire up Visual Studio and create a new C# project.  "First things first, I need library X." you say.  "Wait, what the?"


The full error:

 Package 'MyPackage' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8, .NETFramework,Version=v4.8.1' instead of the project target framework 'net6.0'. This package may not be fully compatible with your project.


"Ok" you think, "That library is a bit older.  I'll go update the library project to .NET 6 to match my project.  But, where is .NET 6?  


"Ok, what about my new project?  Just as a test, does the warning go away if I set it to an older .NET Framework?  Wait, where are the .NET Framework versions?"

What is going on here?!? Well, if you're new to .NET (or your coffee hasn't kicked in yet), you may not remember that a few years back, with the introduction of Visual Studio 2017, Microsoft revamped project files (.csproj).  In this case, the library project is using the older style, which supports only .NET Framework.  The new project is using the newer .csproj style, which supports .NET Core & .NET 5/6.  Backwards compatibility isn't great, though there is now a tool for converting old projects.

But, here's the kicker.  Finding this issue may not be quite that easy.  If you miss the warning in Solution Explorer, the project will still compile.  In my case, it was a test project, and I had no issues until I got this at runtime:

System.IO.FileNotFoundException : Could not load file or assembly 'System.Windows.Forms, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.
TearDown : System.InvalidOperationException : The host address has not been set.

The library had the dependency on System.Windows.Forms, but it couldn't find it due to the aforementioned warning.  Legacy code is difficult to shake.

One other tidbit on old projects: If trying to run nuget pack on a legacy project file, you could get an error like this:

Method not found: 'System.ReadOnlySpan`1<Char> Microsoft.IO.Path.GetFileName(System.ReadOnlySpan`1<Char>)'.

In my case, I had to download a very old version of the NuGet executable (v3.4.4) to get it to run.


Popular posts from this blog

Fixing Conan Lock Issues

Making a standard ASP.NET listbox do multiselect without holding Ctrl

Setting up Jenkins, GoogleTest, & Mercurial (with a local repository)