.NET Core 和 .Net 4.5.2 使用的通用 class 库
A common class library consumed by both .NET Core and .Net 4.5.2
我是 .Net Core 的新手,但已经创建了一个可用的 Asp.Net Core WebAPI 站点 - 现在我想与另一个项目共享一些代码...
- 我有 Visual Studio 2015 安装了 Update 3。
- 我从 here.
安装了 DotNetCore.1.0.0-VS2015Tools.Preview2.exe
我想创建一个可供其他两个库使用的共享库 (PCL) - 它只包含原语 classes/interfaces,没有其他依赖项。其中一个消费库是针对 "netstandard1.6" 的新香草项目,另一个是针对 .Net 4.5.2 的旧客户端库(如果必须,我可以将其升级到 4.6.x)。
我一直在兜圈子,我无法让 netstandard1.6 库引用 PCL - 我只是被告知缺少类型:
Error CS0246: The type or namespace name 'SomeTypeHere' could not be found (are you missing a using directive or an assembly reference?)
名为 "ClassLibrary1" 的 project.json 的 PCL 自动生成为:
{
"supports": {},
"dependencies": {
"Microsoft.NETCore.Portable.Compatibility": "1.0.1",
"NETStandard.Library": "1.6.0"
},
"frameworks": {
"netstandard1.1": {}
}
}
我的消费库 project.json 是:
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.0",
"Newtonsoft.Json": "9.0.1"
},
"frameworks": {
"netstandard1.6": {
"dependencies": {
"ClassLibrary1": {
"target": "project"
}
}
}
}
}
我怎样才能使这个工作?
编辑 2016 年 7 月 7 日:
我已经提供了以下解决方案,它演示了我的设置:
https://github.com/JonnyWideFoot/netcore-prototype
请参阅 ExperimentClient::GetLocationAsync 了解我想在 .Net 4.5.2 / 4.6.x 客户端中使用合同库的位置。
以下是我创建可从 .NET Core 项目和 .NET 4.5 项目中使用的共享库的方法:
SharedLibrary\project.json
"dependencies": { },
"frameworks": {
"net45": { },
"netstandard1.1": {
"dependencies": {
"NETStandard.Library": "1.6.0"
}
}
},
"version": "1.0.0"
同一解决方案中的一个消费类 (.NET Core) 库是这样引用它的:
"dependencies": {
"SharedLibrary": {
"target": "project",
"version": "1.0.0"
}
},
"frameworks": {
"netstandard1.1": { }
}
}
使用 project.json
的消耗性 .NET 4.5 项目看起来与框架部分中的 net45
不同。如果生成了 SharedLibrary 的 NuGet 包,则在基于 csproj
的 .NET 4.5 项目中安装也可以。
根据 .NET Platform Standard docs,简单地定位 netstandard1.1
应该允许共享库也安装在 .NET 4.5+ 项目中。我 运行 遇到了奇怪的问题,但这可能是测试版工具的结果。
我发现使这项工作有效的唯一方法是破解引用客户端库的 .csproj 文件:
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.Contract\bin$(Configuration)\net452\JE.Api.Experiment.Contract.dll</HintPath>
</Reference>
通过硬编码从合同库到正确输出文件夹的路径,一切都很好。
...认为这可能是 visual studio 中的错误。
我是 .Net Core 的新手,但已经创建了一个可用的 Asp.Net Core WebAPI 站点 - 现在我想与另一个项目共享一些代码...
- 我有 Visual Studio 2015 安装了 Update 3。
- 我从 here. 安装了 DotNetCore.1.0.0-VS2015Tools.Preview2.exe
我想创建一个可供其他两个库使用的共享库 (PCL) - 它只包含原语 classes/interfaces,没有其他依赖项。其中一个消费库是针对 "netstandard1.6" 的新香草项目,另一个是针对 .Net 4.5.2 的旧客户端库(如果必须,我可以将其升级到 4.6.x)。
我一直在兜圈子,我无法让 netstandard1.6 库引用 PCL - 我只是被告知缺少类型:
Error CS0246: The type or namespace name 'SomeTypeHere' could not be found (are you missing a using directive or an assembly reference?)
名为 "ClassLibrary1" 的 project.json 的 PCL 自动生成为:
{
"supports": {},
"dependencies": {
"Microsoft.NETCore.Portable.Compatibility": "1.0.1",
"NETStandard.Library": "1.6.0"
},
"frameworks": {
"netstandard1.1": {}
}
}
我的消费库 project.json 是:
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.0",
"Newtonsoft.Json": "9.0.1"
},
"frameworks": {
"netstandard1.6": {
"dependencies": {
"ClassLibrary1": {
"target": "project"
}
}
}
}
}
我怎样才能使这个工作?
编辑 2016 年 7 月 7 日:
我已经提供了以下解决方案,它演示了我的设置: https://github.com/JonnyWideFoot/netcore-prototype 请参阅 ExperimentClient::GetLocationAsync 了解我想在 .Net 4.5.2 / 4.6.x 客户端中使用合同库的位置。
以下是我创建可从 .NET Core 项目和 .NET 4.5 项目中使用的共享库的方法:
SharedLibrary\project.json
"dependencies": { },
"frameworks": {
"net45": { },
"netstandard1.1": {
"dependencies": {
"NETStandard.Library": "1.6.0"
}
}
},
"version": "1.0.0"
同一解决方案中的一个消费类 (.NET Core) 库是这样引用它的:
"dependencies": {
"SharedLibrary": {
"target": "project",
"version": "1.0.0"
}
},
"frameworks": {
"netstandard1.1": { }
}
}
使用 project.json
的消耗性 .NET 4.5 项目看起来与框架部分中的 net45
不同。如果生成了 SharedLibrary 的 NuGet 包,则在基于 csproj
的 .NET 4.5 项目中安装也可以。
根据 .NET Platform Standard docs,简单地定位 netstandard1.1
应该允许共享库也安装在 .NET 4.5+ 项目中。我 运行 遇到了奇怪的问题,但这可能是测试版工具的结果。
我发现使这项工作有效的唯一方法是破解引用客户端库的 .csproj 文件: 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.Contract\bin$(Configuration)\net452\JE.Api.Experiment.Contract.dll</HintPath>
</Reference>
通过硬编码从合同库到正确输出文件夹的路径,一切都很好。
...认为这可能是 visual studio 中的错误。