Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference .NET 4.5 dll in .NET Core 1.1 csproj?

I'm running VS 2017 RC4.

I add a reference in my .NET Core app to my .NET 4.5 dll and it compiles. When a line that references the dll is called at runtime, I get:

System.IO.FileNotFoundException: 'Could not load file or assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.'

This image shows that to use 4.5 references, I need to use netstandard 1.1. https://msdnshared.blob.core.windows.net/media/2016/07/172.png

Assuming that is what I need, how do I reference it in my .csproj? I can only find old documentation for when project.json was used instead.

I tried adding the below but it did not help:

<NetStandardImplicitPackageVersion>1.1</NetStandardImplicitPackageVersion>

Also, I need to add:

<RuntimeFrameworkVersion>1.0.3</RuntimeFrameworkVersion>

Or I get FileNotFoundException: Could not load file or assembly. The system cannot find the file specified.

Why is that?

Here are the relevant parts of my .csproj:

<PropertyGroup>
    <TargetFramework>netcoreapp1.1</TargetFramework>
    <PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback>
<RuntimeFrameworkVersion>1.0.3</RuntimeFrameworkVersion>
  </PropertyGroup>
<ItemGroup>
    <Reference Include="My4.5dll">
      <HintPath>Dlls\My4.5dll.dll</HintPath>
    </Reference>
  </ItemGroup>
like image 880
KevinUK Avatar asked Feb 20 '17 16:02

KevinUK


People also ask

Can I use .NET dll in .NET core?

Can I use . NET framework DLL in . NET core? Attachments: Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Can .NET Core Reference .NET standard?

NET Core 2.0. Because . NET Core 2.0 implements . NET Standard 2.0, it also has the compatibility mode for referencing .

Can I use .NET Framework dll in .NET 6?

Yes, you can load . NET Framework assemblies into . NET Core 5 and 6.


1 Answers

You can't (safely) load a .NET Framework 4.5 library into .NET Core, because it may use APIs which are unavailable in .NET Core.

Only if your library targets portable-net45+win8 (.NET Framework 4.5 and Windows 8 Portable Class Profile or higher) it can be used with .NET Core. Because this specific PCL Profile limits the API which is compatible to (what was formerly called WinRT) System.Runtime, which is what is .NET Core is based on.

For a list of compatible PCL profiles, see this list (PCL Compatibility at the bottom)

If the assembly you want to reference do not support netstandard1.x or any of the supported profiles, you have to target .NET Framework 4.5 instead of .NET Core.

In your csproj

<TargetFramework>net45</TargetFramework>
...
<ItemGroup>
    <PackageReference Include="Net45DependencyHere" Version="4.5.0" />

or if you multi-target

<TargetFrameworks>net45;netcoreapp1.1</TargetFrameworks>
...
<ItemGroup>
    <PackageReference Condition="'$(TargetFramework)' == 'net45' Include="Net45DependencyHere" Version="4.5.0" />
    <PackageReference Condition="'$(TargetFramework)' == 'netcoreapp1.1' Include="NetCoreReplacementLibrary" Version="1.1.0" />

You just can't auto-magically use any .NET Framework 4.5 library in your .NET Core project. Only PCL and netstandard1.x ones.

Update

For the sake of completeness:

If you are certain that your class library/package targets a compatible PCL, you can make nuget restore this packages too, even if they don't target netstandard1.x.

<PropertyGroup>
    <PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback>
</PropertyGroup>

Word of warning

Never, I repeat, NEVER put anything else there except for compatible PCL Libraries. NEVER put net45 in here. This will just force NuGet to download and install this package, but it won't make it work and crash at runtime with similar error like you have above!

It's only there to force nuget to install package which are know to work with .NET Core for the transition period until most packages target netstandard1.x!

like image 101
Tseng Avatar answered Sep 21 '22 09:09

Tseng