.NET Core 和 .Net 4.5.2 使用的通用类库 [英] A common class library consumed by both .NET Core and .Net 4.5.2

查看:49
本文介绍了.NET Core 和 .Net 4.5.2 使用的通用类库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 .Net Core 还很陌生,但已经创建了一个可用的 Asp.Net Core WebAPI 站点 - 现在我想与另一个项目共享一些代码...

I'm fairly new to .Net Core, but have made a working Asp.Net Core WebAPI site - now I want to share some code with another project...

  • 我安装了带有 Update 3 的 Visual Studio 2015.
  • 我从这里安装了 DotNetCore.1.0.0-VS2015Tools.Preview2.exe.
  • I have Visual Studio 2015 with Update 3 installed.
  • I have DotNetCore.1.0.0-VS2015Tools.Preview2.exe installed from here.

我想创建一个可供其他两个库使用的共享库 (PCL) - 它只包含基本类/接口,没有其他依赖项.其中一个消耗库是针对netstandard1.6"的新 vanilla 项目,另一个是针对 .Net 4.5.2 的旧客户端库(如果需要,我可以将其升级到 4.6.x).

I would like to create a shared library (PCL) that can be consumed by two other libraries - it only contains primitive classes/interfaces with no other dependencies. One of the consuming libraries is a new vanilla project targeting "netstandard1.6", the other is an old client library which targets .Net 4.5.2 (I can upgrade this to 4.6.x if I must).

我一直在绕圈子,我无法让 netstandard1.6 库引用 PCL - 我只是被告知缺少类型:

I've been round in circles, and I can't make the netstandard1.6 library reference the PCL - I just get told the types are missing:

错误 CS0246:找不到类型或命名空间名称SomeTypeHere"(您是否缺少 using 指令或程序集引用?)

Error CS0246: The type or namespace name 'SomeTypeHere' could not be found (are you missing a using directive or an assembly reference?)

名为ClassLibrary1"的 PCL 的 project.json 自动生成为:

The PCL named "ClassLibrary1"'s project.json is auto-generated as:

{
  "supports": {},
  "dependencies": {
    "Microsoft.NETCore.Portable.Compatibility": "1.0.1",
    "NETStandard.Library": "1.6.0"
  },
  "frameworks": {
    "netstandard1.1": {}
  }
}

我使用的库 project.json 是:

My consuming library project.json is:

{
  "version": "1.0.0-*",
  "dependencies": {
    "NETStandard.Library": "1.6.0",
    "Newtonsoft.Json": "9.0.1"
  },
  "frameworks": {
    "netstandard1.6": {
      "dependencies": {
        "ClassLibrary1": {
          "target": "project"
        }
      }
    }
  }
}  

我怎样才能做到这一点?

How can I make this work?

编辑 07/07/2016:

EDIT 07/07/2016:

我提供了以下解决方案,它演示了我的设置:https://github.com/JonnyWideFoot/netcore-prototype请参阅 ExperimentClient::GetLocationAsync 以了解我想在 .Net 4.5.2/4.6.x 客户端中使用合约库的位置.

I have made the following solution available, which demonstrates my setup: https://github.com/JonnyWideFoot/netcore-prototype See ExperimentClient::GetLocationAsync for where I would like to use the Contracts Library within the .Net 4.5.2 / 4.6.x Client.

推荐答案

我发现使这项工作有效的唯一方法是 hack 引用客户端库的 .csproj 文件:https://github.com/JonnyWideFoot/netcore-prototype/blob/master/src/JE.API.Experiment.Client/JE.API.Experiment.Client.csproj

The only way I have found to make this work, is to hack reference the .csproj file of the Client library: https://github.com/JonnyWideFoot/netcore-prototype/blob/master/src/JE.API.Experiment.Client/JE.API.Experiment.Client.csproj

<代码><Reference Include="JE.Api.Experiment.Contract, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL"><SpecificVersion>False</SpecificVersion><HintPath>..JE.Api.Experiment.Contractin$(Configuration) et452JE.Api.Experiment.Contract.dll</HintPath></参考>

<Reference Include="JE.Api.Experiment.Contract, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL"> <SpecificVersion>False</SpecificVersion> <HintPath>..JE.Api.Experiment.Contractin$(Configuration) et452JE.Api.Experiment.Contract.dll</HintPath> </Reference>

通过对合同库中正确输出文件夹的路径进行硬编码,一切都很好.

By hard-coding the path to the correct output folder from the contracts library, all is fine.

...认为这可能是 Visual Studio 中的错误.

... thinking this could be a bug in visual studio.

这篇关于.NET Core 和 .Net 4.5.2 使用的通用类库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆