从.NET Core项目中引用一个可移植类库

3

从.NET Core项目中添加对PCL(目标为.NET 4.6和.NET Core)的引用是否可能?

我尝试创建了这两种类型的项目,但是如果我从.NET Core项目向PCL添加引用,它无法识别任何名称空间。但是它能够编译成功。

下面是project.json文件:

PCL:

{
  "supports": {
    "net46.app": {},
    "dnxcore50.app": {}
  },
  "dependencies": {
    "Microsoft.NETCore": "5.0.0",
    "Microsoft.NETCore.Portable.Compatibility": "1.0.0"
  },
  "frameworks": {
    "dotnet": {
      "imports": "portable-net452"
    }
  }
}

.NET Core:

{
  "version": "1.0.0-*",

  "dependencies": {
    "NETStandard.Library": "1.6.0"
  },

  "frameworks": {
    "netstandard1.6": {
      "dependencies": {
        "ClassLibrary1": { // <-- Reference to the PCL
          "target": "project"
        }
      },
      "imports": "dnxcore50"
    }
  }
}

编辑:

不知道是否有用,这里是所请求的构建输出:

Restoring NuGet packages...
To prevent NuGet from restoring packages during build, open the Visual Studio Options dialog, click on the Package Manager node and uncheck 'Allow NuGet to download missing packages during build.'
1>------ Build started: Project: ClassLibrary2, Configuration: Debug Any CPU ------
1>  C:\Program Files\dotnet\dotnet.exe build "C:\ClassLibrary2\ClassLibrary2" --configuration Debug --no-dependencies
1>  Project ClassLibrary2 (.NETStandard,Version=v1.6) will be compiled because inputs were modified
1>  Compiling ClassLibrary2 for .NETStandard,Version=v1.6
1>  Compilation succeeded.
1>      0 Warning(s)
1>      0 Error(s)
1>  Time elapsed 00:00:00.8914597
1>
========== Build: 1 succeeded, 0 failed, 1 up-to-date, 0 skipped ==========

编辑 2:

好吧,我至少已经成功构建了它,但现在我遇到了一个关于无法找到System.Runtime的运行时错误。奇怪的是,在测试解决方案中它可以工作,但不能在我想要实施它的解决方案中工作。

供参考,这里是我的project.json文件:

PCL:

{
  "supports": {},
  "dependencies": {
    "Microsoft.CSharp": "4.0.1",
    "Microsoft.NETCore.Portable.Compatibility": "1.0.1",
    "NETStandard.Library": "1.6.0",
    "System.Runtime.Serialization.Primitives": "4.1.1"
  },
  "frameworks": {
    "net451": {},
    "netstandard1.5": {}
  }
}

.NET Core:

{
  "version": "1.0.0-*",

  "dependencies": {
    "NETStandard.Library": "1.6.0"
  },

  "frameworks": {
    "netstandard1.6": {
      "imports": "dnxcore50",
      "dependencies": {
        "PclTest": {
          "target": "project"
        },
        "System.Runtime": "4.1.0"
      }
    }
  }
}

你有编译器/构建输出吗? - oɔɯǝɹ
怪异。https://dev59.com/JZffa4cB1Zd3GeqP3iIx 有人遇到了和你一样的问题,并且刚刚问了这个问题。他们的问题得到了更多的关注。去看看答案是否适用于你。 - user1228
https://github.com/aspnet/Home/issues/1356 - user1228
@Will 谢谢你的回复。看起来目前还不可能实现。 - Tom
如果您有重现步骤,请务必通过 Github 或 Connect 与核心团队联系。 - user1228
1个回答

2
我相信这是因为Visual Studio混淆了RC1和RTM项目的组合,它不能识别任何命名空间(namespace)(尽管我不知道结果是否实际可用)。如果您想编写一个可以从.Net Core和.Net Framework中使用的库,应该使用netstandard而不是dotnet。您的“PCL”ClassLibrary1的project.json应该与“.Net Core”ClassLibrary2的类似。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接