6

My C# assemblies currently all target .NET Core 3.1. They all use my C++/CLI assembly (call it "MySdk") that targets .NET Framework 4.8. This works fine.

Today I tried updating the C# assemblies to .NET 5.0. Suddenly, none of them even see the .NET FW 4.8 "MySdk" assembly that's already built and sitting right there. The build process gives me output like this:

> 2>------ Build started: Project: MyApp.Core, Configuration: Debug x64
> ------ 2>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2123,5):
> warning MSB3245: Could not resolve this reference. Could not locate
> the assembly "MySdk". Check to make sure the assembly exists on disk.
> If this reference is required by your code, you may get compilation
> errors.

The obvious solution is to update the C++/CLI assembly to .NET 5. And I want to do that soon, though I don't know if it's possible.

But let us suppose that's not an option right now. Is it even valid for a .NET 5 assembly to use a .NET FW 4.8 assembly? Should this work? And is there anything else I can do to make my .NET 5 assemblies see and use this thing?

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
Joe
  • 5,394
  • 3
  • 23
  • 54
  • 2
    No. .NET 5 is .NET **Core** 5 – Panagiotis Kanavos Nov 12 '20 at 16:37
  • Then why do the .NET Core 3.1 versions of the same assemblies work just fine with the .NET Framework 4.8 assembly? – Joe Nov 12 '20 at 16:39
  • Are you 100% certain that your C++/CLI project is targeting Framework and not Core? Because according to everything I know about this, Core should **not** be able to reference Framework C++/CLI, regardless of the version. – Ian Kemp Nov 12 '20 at 17:46
  • I am. "Project >> Settings >> Advanced >> .NET Target Framework Version" is 4.8. And I am using some types in System::Windows::Media that are not in any compatibility pack for .NET Core or .NET 5 – Joe Nov 12 '20 at 17:49
  • I should add that I am using the the NuGet "Microsoft.Windows.Compatibility" pack with this C++/CLI assembly to make this happen. Tried upgrading it from 3.1 to 5.0.0 a moment ago but it didn't help. C# assemblies still don't see the 4.8 assembly – Joe Nov 12 '20 at 17:50
  • 1
    yes .Net 5.0 can use a 4.8 assembly. You probably have to be careful if the assembly uses a bunch of other references that are not Core compatible. I had issues when I was mixing one that used Newtonsoft for .Net 4.8, the dlls get mixed. – hal9000 Mar 12 '21 at 16:04

1 Answers1

15

The cause of my confusion turns out to be so dumb that I am embarrassed to post it. But I will in case anyone else makes the same mistake I did.

When you update a .NET Core 3.1 app to .NET 5.0, the default build output subfolder changes from "netcoreapp3.1" to "net5.0-windows". But my build script that copied the separately-built C++/CLI assembly was copying it to the old subfolder for .NET Core 3.1 ("netcoreapp3.1").

In other words, it had nothing to do with compatibility. The assembly was being copied to the wrong place

So the answer to my question is "yes".

Joe
  • 5,394
  • 3
  • 23
  • 54
  • 2
    Indirectly, you've answered my own curiosity about whether .NET 5 can reference a 4.8 dll. Thanks. This is the second-to-top result in my bing search for "can .net 3.1 consume .net 4.8 dlls?" – Dale Barnard Mar 22 '21 at 14:37