We're a bit behind the times with all our products (WPF applications) still using v4.6.1. I'm currently having a "play" with .NET 5, and have converted one of our class libraries (used by many of these products) to v5. I'm now attempting to reference this assembly from one of our smaller products, but it's not building, with lots of errors such as:
Dependency "System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a". 1> Could not resolve this reference. Could not locate the assembly "System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
I was under the impression .NET 5 was backwards compatible, or does that only apply to .NET Standard? (TBH I've always struggled to get my head around the whole .NET Standard/Core thing).
We don't want to take the plunge and convert the products to .NET 5 just yet - that's a long-term strategy. It would have been nice to have the above class library converted ready for when we do start, so is there any way that a .NET framework solution can reference the .NET 5 assembly? I'm guessing this is impossible, and the only option would be to multi-target? If so how do I do this, and will it go back as far as v4.6.1? Or are we stuck with the "big bang" approach of converting everything to .NET 5 at the same time?
Not sure if it's relevant but in the \bin\debug\
folder of the class library I can see various subfolders such as net5.0
, net5.0-windows
, net472
, netstandard2.1
- only net5.0-windows
contains the built DLL. We may be able to work with 4.7.2 if that's the minimum it'll go back to, but I can't figure out how to multi-target in the first place. In the "Target framework" dropdown I only see .NET 5.0, .NET Core 3.0, .NET Core 3.1.
Edit 1
I followed Camilo's suggestion below to replace with:
<TargetFrameworks>net5.0-windows;net461</TargetFrameworks>
Now, the code won't build. Looking at one example it seems to be complaining that it can find the type "ItemsControl" for .Net5 but not 4.6.1:
I'm assuming I need to add System.Windows.Controls for v4.6.1 somehow, although this is unintuitive at best - as there is no Add Reference -> Assemblies window that you'd find in a .Net framework application. I'm assuming this must now be done via NuGet, and if so which package do I need?
Developer Community. In Visual Studio for Mac if you try to reference a.NET Framework project from a . NET Standard project. the IDE won't let you do it due to incompatible target frameworks, but in Visual Studio 2019 on Windows, you can add that reference.
NET Core, you need to use the . NET Framework. You can now reference . NET Framework libraries from .
One method of adding references to your library is by typing it directly in the project. json file. As you can see that we have added some references under the dependencies section as shown in the following code. Let us now save this file and you will see that references are added to your library now.
In the Project Designer, click the References tab. Click the Add button to open the Add Reference dialog box. In the Add Reference dialog box, select the tab indicating the type of component you want to reference. Select the components you want to reference, and then click OK.
.NET Standard 2.0 is the last target framework compatible with .NET Framework. .NET Standard 2.1 is only .NET Core 3.0 and newer. .NET 5 is compatible with .NET 5 and newer (when they come out).
Do notice that the WPF port became available in .NET Core 3.0, so that's why you cannot target any version below it.
Let's see some options:
The library should target .NET Standard 2.0:
<TargetFramework>netstandard2.0</TargetFramework>
The library would target both .NET Framework 4.6.1 and .NET 5:
<TargetFrameworks>net461;net5.0</TargetFrameworks>
Notice that you may need to add #if
directives in many places, for example (however, don't quote me on the identifiers):
#if NET461 // something that only exists in .NET Framework 4.6.1
#elif NET50 // something that only exists in .NET 5.0
#endif
Another problem you can face is that references cannot be found in one of the targets. In this case, you can conditionally import the references (you can look at the Microsoft Docs document for a class to find the library where it lives) like this:
<ItemGroup Condition="'$(TargetFramework)' == 'net461'">
<Reference Include="PresentationCore" />
</ItemGroup>
Create a second project, target only .NET 5, copy over the parts of the code that can be reused and start from scratch what cannot be moved over.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With