16

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>

2 Answers 2

15

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!

Sign up to request clarification or add additional context in comments.

3 Comments

While this didn't help my particular case, I tested it and noticed the following problem: The TargetFramework value 'net45;netcoreapp2.0' is not valid. To multi-target, use the 'TargetFrameworks' property instead. So I guess an extra s is needed in the <TargetFramework> property.
@WouterVanherck: Correct. Copy & paste mistake. Fixed that
Thanks for all the help, I only had to modify the .Net Framwork version from 45 to 452. Shared the details here dotnetinternal.blogspot.com/2018/10/…
0

I had the same problem, but with .NET Standard 2.0 and a .NET Framework 4.0 assembly. The solution was create a nuget with my dll in nuget package explorer targeting .NET Standard 2.0, then add the package to the project.

Link to nuget explorer: https://github.com/NuGetPackageExplorer/NuGetPackageExplorer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.