简体   繁体   English

参考.NET Core 1.1 csproj中的.NET 4.5 dll?

[英]Reference .NET 4.5 dll in .NET Core 1.1 csproj?

I'm running VS 2017 RC4. 我正在运行VS 2017 RC4。

I add a reference in my .NET Core app to my .NET 4.5 dll and it compiles. 我将.NET Core应用程序中的引用添加到我的.NET 4.5 dll中并进行编译。 When a line that references the dll is called at runtime, I get: 当在运行时调用引用dll的行时,我得到:

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. 此图显示要使用4.5引用,我需要使用netstandard 1.1。 https://msdnshared.blob.core.windows.net/media/2016/07/172.png 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? 假设这是我需要的,我如何在我的.csproj中引用它? I can only find old documentation for when project.json was used instead. 我只能在使用project.json时找到旧文档。

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. 或者我得到FileNotFoundException:无法加载文件或程序集。 The system cannot find the file specified. 该系统找不到指定的文件。

Why is that? 这是为什么?

Here are the relevant parts of my .csproj: 以下是我的.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>

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. 您无法(安全地)将.NET Framework 4.5库加载到.NET Core中,因为它可能使用.NET Core中不可用的API。

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. 只有当您的库定位于portable-net45+win8 (.NET Framework 4.5 Windows 8 Portable Class Profile或更高版本)时,它才能与.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. 因为这个特定的PCL配置文件限制了与(以前称为WinRT) System.Runtime兼容的API,这是.NET Core所基于的。

For a list of compatible PCL profiles, see this list (PCL Compatibility at the bottom) 有关兼容PCL配置文件的列表,请参阅此列表 (底部的PCL兼容性)

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. 如果要引用的程序集不支持netstandard1.x或任何受支持的概要文件,则必须以 .NET Framework 4.5而不是.NET Core 目标。

In your csproj 在你的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. 您无法在.NET Core项目中自动神奇地使用任何.NET Framework 4.5库。 Only PCL and netstandard1.x ones. 只有PCL和netstandard1.x

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 . 如果您确定类库/包的目标兼容的PCL,可以使的NuGet恢复这个包太大,即使他们不针对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. 从来没有 ,我再说一遍, 从来没有把什么都没有,除了兼容的PCL库。 NEVER put net45 in here. 永远不要net45放在这里。 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! 这将迫使NuGet下载并安装此软件包,但它不会使它在运行时崩溃并出现类似的错误,就像你上面一样!

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 ! 只有那里才能强制nuget安装包,这些包知道可以在过渡期使用.NET Core,直到大多数软件包都以netstandard1.x目标!

I had the same problem, but with .NET Standard 2.0 and a .NET Framework 4.0 assembly. 我有同样的问题,但使用.NET Standard 2.0和.NET Framework 4.0程序集。 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. 解决方案是使用我的dll在nuget包浏览器中创建一个nuget,目标是.NET Standard 2.0,然后将包添加到项目中。

Link to nuget explorer: https://github.com/NuGetPackageExplorer/NuGetPackageExplorer 链接到nuget资源管理器: https//github.com/NuGetPackageExplorer/NuGetPackageExplorer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM