在 NetStandard1.3 项目中引用 PCLCrypto

Referencing PCLCrypto in a NetStandard1.3 project

我正在尝试在使用 NetStandard 1.3 的项目中添加对包 PCLCrypto (2.0.147) 的引用

在 project.json 中添加导入 "portable-net45+netcore45+wpa81" 后,它会生成,但一直显示错误。


project.json:

{
  "supports": {},
  "dependencies": {
    "Microsoft.EntityFrameworkCore": "1.1.1",
    "NETStandard.Library": "1.6.1",
    "PCLCrypto": "2.0.147",
    "System.ComponentModel.Primitives": "4.1.0",
    "WraUtil.Helpers": "1.8.2"
  },
  "frameworks": {
    "netstandard1.3": {
      "imports": "portable-net45+netcore45+wpa81"
    }
  }
}

错误:

Severity    Code    Description Project File    Line    Suppression State
Error       Package PInvoke.NCrypt 0.3.2 is not compatible with netstandard1.3 (.NETStandard,Version=v1.3). Package PInvoke.NCrypt 0.3.2 supports:
  - net40 (.NETFramework,Version=v4.0)
  - portable-net40+win8+wpa81 (.NETPortable,Version=v0.0,Profile=Profile92)
  - portable-net45+win8+wpa81 (.NETPortable,Version=v0.0,Profile=Profile111)
Error       Package Validation 2.2.8 is not compatible with netstandard1.3 (.NETStandard,Version=v1.3). Package Validation 2.2.8 supports:
  - dotnet (.NETPlatform,Version=v5.0)
  - portable-dnxcore50+monoandroid10+monotouch10+net45+win+wp8+wpa81+xamarinios10 (.NETPortable,Version=v0.0,Profile=net45+dnxcore50+win+wpa81+wp80+MonoAndroid10+xamarinios10+MonoTouch10)
  - portable-net40+sl5+win8+wp8+wpa81 (.NETPortable,Version=v0.0,Profile=Profile328)
Error       Package PCLCrypto 2.0.147 is not compatible with netstandard1.3 (.NETStandard,Version=v1.3). Package PCLCrypto 2.0.147 supports:
  - monoandroid23 (MonoAndroid,Version=v2.3)
  - monotouch10 (MonoTouch,Version=v1.0)
  - net45 (.NETFramework,Version=v4.5)
  - portable-net45+win8+wp8+wpa81 (.NETPortable,Version=v0.0,Profile=Profile259)
  - portable-win81+wpa81 (.NETPortable,Version=v0.0,Profile=Profile32)
  - wp8 (WindowsPhone,Version=v8.0)
  - xamarinios10 (Xamarin.iOS,Version=v1.0)
Error       One or more packages are incompatible with .NETStandard,Version=v1.3.
Error       Package PCLCrypto 2.0.147 is not compatible with netstandard1.3 (.NETStandard,Version=v1.3). Package PCLCrypto 2.0.147 supports:
  - monoandroid23 (MonoAndroid,Version=v2.3)
  - monotouch10 (MonoTouch,Version=v1.0)
  - net45 (.NETFramework,Version=v4.5)
  - portable-net45+win8+wp8+wpa81 (.NETPortable,Version=v0.0,Profile=Profile259)
  - portable-win81+wpa81 (.NETPortable,Version=v0.0,Profile=Profile32)
  - wp8 (WindowsPhone,Version=v8.0)
  - xamarinios10 (Xamarin.iOS,Version=v1.0)
Error       One or more packages are incompatible with .NETStandard,Version=v1.3.
Error       Package Validation 2.2.8 is not compatible with netstandard1.3 (.NETStandard,Version=v1.3). Package Validation 2.2.8 supports:
  - dotnet (.NETPlatform,Version=v5.0)
  - portable-dnxcore50+monoandroid10+monotouch10+net45+win+wp8+wpa81+xamarinios10 (.NETPortable,Version=v0.0,Profile=net45+dnxcore50+win+wpa81+wp80+MonoAndroid10+xamarinios10+MonoTouch10)
  - portable-net40+sl5+win8+wp8+wpa81 (.NETPortable,Version=v0.0,Profile=Profile328)
Error       Package PInvoke.NCrypt 0.3.2 is not compatible with netstandard1.3 (.NETStandard,Version=v1.3). Package PInvoke.NCrypt 0.3.2 supports:
  - net40 (.NETFramework,Version=v4.0)
  - portable-net40+win8+wpa81 (.NETPortable,Version=v0.0,Profile=Profile92)
  - portable-net45+win8+wpa81 (.NETPortable,Version=v0.0,Profile=Profile111)

我还需要配置什么吗?

在你的 project.json 文件的 imports 字段中,你应该放置一个与 PCLCrypto 2.0.147 兼容的目标框架,这些错误基本上告诉你哪个你有的选择。

例如,其中一个受支持的目标框架是 portable-net45+win8+wp8+wpa81,它与 netstandard1.0 兼容,这意味着它也可以被 netstandard1.3 项目引用(您可以找到here 有关旧 PCL 配置文件和新 .NET Standard 版本之间兼容性的更多信息。

因此,将您的 imports 字段更新为:"imports": "portable-net45+win8+wp8+wpa81"

小红利 - 如果您决定从 project.json 迁移到新的 MSBuild (csproj) 样式项目,您可以通过以下方式实现同​​样的效果:

<PropertyGroup>
    <TargetFramework>netstandard1.3</TargetFramework>
    <PackageTargetFallback>portable-net45+win8+wp8+wpa81</PackageTargetFallback>
</PropertyGroup>