简体   繁体   English

dotnet pack 项目参考

[英]dotnet pack project references

I quite like separating functionality across a few assemblies, for example a facade to a data provider, contracts for the data provider and the data provider implementation itself... to my mind, it makes it easy to unit test the individual components of a piece of functionality and easy to swap out one thing in the future (in the case of my example, it makes the data provider easy to swap out).我非常喜欢在几个程序集中分离功能,例如数据提供者的外观、数据提供者的合同和数据提供者实现本身......在我看来,它可以很容易地对单个组件进行单元测试功能,并且将来可以轻松换出一件事(在我的示例中,它使数据提供程序易于换出)。

If I create a solution with 3 projects and use project references, when I dotnet-build on the entry assembly, all the references are copied to the output folder.如果我创建一个包含 3 个项目的解决方案并使用项目引用,当我在入口程序集上进行 dotnet-build 时,所有引用都会复制到输出文件夹中。 When I dotnet pack the entry assembly project to create a NuGET package, only the entry asembly (not the contracts or the data provider) are included in the NuGET package当我dotnet打包入口程序集项目以创建NuGET包时,NuGET包中只包含入口程序集(不是合同或数据提供者)

This appears to be by design;这似乎是有意为之; the documentation for .NET Core dotnet-pack states that .NET Core dotnet-pack 的文档指出

Project-to-project references aren't packaged inside the project.项目到项目的引用不打包在项目中。 Currently, you must have a package per project if you have project-to-project dependencies.目前,如果您有项目到项目的依赖关系,则每个项目必须有一个包。

My question is - why is this the case?我的问题是 - 为什么会这样? If I want to separate my code into logical assemblies, I am forced to either create separate NuGET packages and reference those, or simply lump all my code into a single assembly.如果我想将我的代码分成逻辑程序集,我不得不创建单独的 NuGET 包并引用它们,或者只是将我的所有代码集中到一个程序集中。 Is there any way to include project references in a NuGET package?有没有办法在 NuGET 包中包含项目引用?

I am using VS2017 / .NET Core v1.1 (csproj, not xproj)我使用的是 VS2017 / .NET Core v1.1(csproj,而不是 xproj)

A possible way to achieve the needed is to use a custom .nuspec file where you can specify the dlls you want to be packed实现所需的一种可能方法是使用自定义.nuspec文件,您可以在其中指定要打包的 dll

<PropertyGroup>
    <NuspecFile>App.nuspec</NuspecFile>
</PropertyGroup>

Having this, dotnet pack will produce the package with respect to MyPackage.nuspec .有了这个, dotnet pack将生成关于MyPackage.nuspec的包。

Furthermore, if you have like 3 projects with contracts and implementation and you don't want to add 3 package references, you can create a meta-package that simply has those 3 as dependencies and reference that single meta-package.此外,如果您有 3 个带有合同和实现的项目,并且您不想添加 3 个包引用,则可以创建一个元包,将这 3 个作为依赖项并引用该单个元包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM