【问题标题】:PCL - Targetting .NET 4.5.2 and ASPNetCore 1.0PCL - 针对 .NET 4.5.2 和 ASPNetCore 1.0
【发布时间】:2017-02-28 19:32:15
【问题描述】:

我有一个 PCL,我的目标是 .NET 4.5.2 和 ASP.NET Core 1.0,但每当我从我的 DotNetCore AspNetCore 1.0 应用程序中引用这个 PCL 时。

修复:

原来是 ReSharper 的一个问题,在安装 2017.1 EAP 3 后(从 2016.3 开始)引用共享的 .NET Standard 1.2 项目工作得非常好!

问题

更新

通过将我的 PCL 的 Target Framework 更改为 .NET Framework 4.5 而不是 .NET Framework 4.6 来编译它(不要问我为什么会这样,但肯定想知道为什么)。

现在编译成功,但我的 IDE 没有正确解析引用

原始错误

我得到一个编译错误,说它不兼容。

  1. 是否可以有一个针对 .NET 4.5.2 和 DotNetCore 的 PCL(不使用尚未推出的 .NET Standard)
  2. 你是怎么做到的?
  3. 我可以在最新的 VS 2017 RC 中执行此操作吗? (或者我应该安装 VS2015?)

错误信息

PCL 目标

AspNetCore 中引用的 PCL

AspNetCore 1.0 项目设置

.NET 标准实现

我不能使用 .NET Standard 的原因突出显示如下:

【问题讨论】:

  • 你有 csproj 和 xproj 文件吗?
  • 你到底在说什么? NETStandard 与 .NET Core 一起发布(与 ASP.NET Core 同时发布,2016 年 6 月或 7 月)
  • 您收到的确切错误信息是什么?
  • @Tseng 好的,去告诉我哪个 .NET 核心版本可以完全访问 .NET 4.5.2 API(或至少是 DataTable)。我想我的意思是发布了一个“可用的”非核心 .NET 标准
  • @MichalCiechan:那么您不能使用“NETCoreApp 1.0”作为目标应用程序,您必须在 ASP.NET Core 项目中定位“.NET Framework 4.5”

标签: c# asp.net asp.net-core .net-core portable-class-library


【解决方案1】:

在您的屏幕截图中,您可以看到您的 ASP.NET Core 应用程序的目标是 NETCoreApp1.0

NO CIRCUSTANCES 下,您可以在 .NET Core 中使用 .NET 4.5 库。您必须有一个以netstandard1.x 为目标的库/包(如果最低 API 为 .NET 4.5.1 或 4.5.2,则为 1.2)或以兼容的 PCL(即portable-net45+win8 或类似)为目标的库/包。

如果您需要/必须使用不针对 netstandard1.x 的 .NET 4.5/4.6 库,则必须从 ASP.NET Core 应用程序(而不是PCL) 项目设置为.NET Framework 4.5

我知道,但我必须以 Core 1.0 未实现的 .NET Standard 2.0 为目标。根据 apisof.net,它在 .Net Core 1.0 中被引用为 System.Data.Common,但在 2.0 之前从未进入任何标准!

但是你不能只在 .NET Core 上的 Linux/MacOS 上运行它。这限制了您在 Windows 上使用 ASP.NET Core 应用程序或在 Linux/MacOS 上使用 Mono。

更新

使用 .NETStandard,您还可以同时定位 net451netstandard1.x,并使用预处理指令有条件地将代码编译到其中一个程序集中。

为此,您需要创建一个新的“类库(.NET 标准)”项目并编辑 project.json(如果您使用的是 VS2015)或 csproj (VS2017) 以添加目标。

项目.json

  "frameworks": {
    "netstandard1.2": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ],
      "dependencies": {
        "Some.NetCore.Only.Dependency": "1.2.3"
      }
    },
    "net451": {
      "dependencies": {
        "Some.Net451.Only.Dependency": "1.2.3"
      }
    }
  },

或csproj文件

<PropertyGroup>
  <TargetFramework>netstandard1.2;net451</TargetFramework>
</PropertyGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net451' ">
  <!-- Framework references -->
  <Reference Include="System.Runtime.Serialization" />
  <!-- Framework references -->
  <PackageReference Include="Some.Net451.Only.Dependency" Version="1.2.3" />
  <!-- Projects within solution references -->
  <ProjectReference Include="My.Domain.Core" Version="1.0.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.2' ">
  <PackageReference Include="Some.NetCore.Only.Dependency" Version="1.2.3" />
</ItemGroup>

编译/打包后,它将创建两个二进制文件,一个用于netstandard1.2,一个用于net451

在代码中,使用您熟知的 #if 指令(有关 netstandard 指令列表,请参阅 here

#if NETSTANDARD1_2
using Some.NetStandardOnly.Namespace;
#endif
using System;

#if NET451
public DataTable GetDataTable() 
{
    ...
}
#endif

【讨论】:

  • 我想要一个可以执行#if net else pragma 语句的PCL。因此,我的 PcL 以 .Net 4.5 和 asp.net core 1.0 为目标
  • 您也可以使用netstandard1.2 来做到这一点,您只需同时定位netstandard1.2net451 并使用#if NET451 将代码仅编译到:NET 4.5 程序集中而不是@ 987654340@
  • 我创建了一个仅针对 .NET Standard 1.2 的新库。我创建了一个空类,并尝试从我的 .NETCore 1.0 应用程序中引用它,但没有运气。收到以下错误:Error CS0246 The type or namespace name 'ClassFromPcl' could not be found(are you missing a using directive or an assembly reference?)
  • 大声笑,看起来这是 JetBrains ReSharper 生成的错误。安装了最新的 2017.1 EAP3,它工作正常!上帝,我讨厌站在最前沿!
猜你喜欢
  • 2014-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多