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:<
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
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.
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>
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
!