在单个解决方案中,在 DNX/ASP.NET 5 项目 (project.json/xproj) 和其他 C# 项目 (csproj) 之间共享代码的选项有哪些? [英] What are my options for sharing code between DNX / ASP.NET 5 projects (project.json / xproj) and other C# projects (csproj) within a single solution?

查看:18
本文介绍了在单个解决方案中,在 DNX/ASP.NET 5 项目 (project.json/xproj) 和其他 C# 项目 (csproj) 之间共享代码的选项有哪些?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Visual Studio 2015 RC,特别关注切换到新的 ASP.NET 5 框架、项目结构和运行 ASP.NET 5 应用程序的新 DNX.

I'm experimenting with Visual Studio 2015 RC, specifically looking at switching to the new ASP.NET 5 framework, project structure and the new DNX that runs ASP.NET 5 applications.

我的雇主有许多针对 .NET Framework 4.5.2 的现有解决方案.在我们现有的 Visual Studio 解决方案中,我们可能有以下项目:

My employer has many existing solutions targeting .NET Framework 4.5.2. In our existing Visual Studio solutions we might have the following projects:

[Solution] Sample.sln
    [Folder] src
        [Project] ClassLibrary.csproj
        [Project] WindowsService.csproj
        [Project] WebApplication.csproj
    [Folder] test
        [Project] ClassLibrary.UnitTests.csproj
        ...

在这种情况下,ClassLibrary.csproj 是一个包含共享代码的 C# 类库.它是/被WindowsService.csprojWebApplication.csproj 引用的依赖项.

In this scenario, ClassLibrary.csproj is a C# class library containing shared code. It is a dependency of / referenced by both WindowsService.csproj and WebApplication.csproj.

我们并不是专门针对 .NET Core,dnxcore50.在这个阶段,我们很高兴以 .NET Framework 为目标,dnx451.但是,我们绝对会尝试利用 ASP.NET 5 的新功能和相关的项目结构.

We aren't specifically trying to target .NET Core, dnxcore50. At this stage we are happy targeting .NET Framework, dnx451. However we are absolutely trying to leverage the new features of ASP.NET 5 and associated project structure.

以下是我想到的一些选项,但都存在问题.

Below are some options I've thought of but both have issues.

当我们用新的 ASP.NET 5 DNX 项目 WebApplication-dnx 替换上面的 WebApplication.csproj 项目时,我们仍然可以引用 ClassLibrary.csproj 来自这个新的 DNX 项目和现有的 WindowsService.csproj 项目.但是出现了一些问题:

When we replace the WebApplication.csproj project above with a new ASP.NET 5 DNX project WebApplication-dnx then we can still reference the ClassLibrary.csproj from both this new DNX project and the existing WindowsService.csproj project. However some issues arise:

  1. 这种方法意味着对 ClassLibrary.csproj 中代码的任何更改都需要重新构建才能在运行的 WebApplication-dnx 中可见.这并不奇怪,但意味着我们无法从 WebApplication-dnx 获得完整的从源代码编译的好处.

  1. This approach means any changes to code in ClassLibrary.csproj need a rebuild to be visible in the running WebApplication-dnx. This isn't surprising but means we do not get full compile-from-source benefits for WebApplication-dnx.

我们不能轻易针对其他框架,例如dnxcore50.如上所述,这并不是现阶段的具体目标.

We cannot easily target other frameworks, e.g. dnxcore50. As above, this isn't specifically a goal at this stage.

选项 2

如果我们用 DNX 类库项目替换 ClassLibrary.csproj ^ ClassLibrary-dnx 那么选项 1 中的问题不适用.这种方法似乎更符合 .NET 运行时和相关技术(如 ASP.NET)的打包方式.

Option 2

If we replace ClassLibrary.csproj with a DNX class library project^ ClassLibrary-dnx then the problems in option 1 do not apply. This approach would seem to be more in-keeping with the way the .NET runtime and associated technologies such as ASP.NET will be packaged moving forward.

但是我找不到从 WindowsService.csproj 引用 ClassLibrary-dnx 的方法.如果这种方法可行,我想该解决方案与 Produce output on build 的项目级别选项有关,然后引用 .nupkg 甚至 >.dll 在构建过程中产生.但是,我看不到通过工具实现这一目标的干净方法.

However I cannot find a way to reference ClassLibrary-dnx from WindowsService.csproj. If this approach is viable I imagine the solution has something to do with the project level-option for Produce outputs on build and then referencing the .nupkg or perhaps even the .dll that is produced during the build. However, I cannot see a clean way of achieving this via the tooling.

^ DNX 类库项目在 VS 2015 RC 中称为 Class Library (Package).此项目类型以前在 CTP 中称为 ASP.NET 类库.

^ DNX class library projects are called Class Library (Package) in VS 2015 RC. This project type was previously called ASP.NET Class Library in the CTPs.

根据以上信息,我正在寻找:

Based on the information above I'm looking for:

  1. 对我的场景选项和问题的一些反馈.

  1. Some feedback to my scenario options and issues.

关于我没有想到的其他选项的建议.

Suggestions for other options I have not thought of.

也许可以指明应该使用哪种方法向前推进.

Perhaps an indication of which approach should be used moving forward.

推荐答案

看看 EntityFramework 做了什么.

他们针对 3 个 TFM:net45、.NETPortable、Version=v4.5、Profile=Profile7、frameworkAssemblies 并且他们同时拥有 csproj 和 xproj 在同一文件夹中.

They target 3 TFMs: net45, .NETPortable,Version=v4.5,Profile=Profile7, frameworkAssemblies and they have both csproj and xproj in the same folder.

基本上,每个项目都有两个项目文件.

Basically, for each project you have two project files.

但是我找不到从 WindowsService.csproj 引用 ClassLibrary-dnx 的方法.

However I cannot find a way to reference ClassLibrary-dnx from WindowsService.csproj.

不幸的是,这还不可能.您只能从 xproj 引用 csproj,反之则不行.您有两种选择:(1) 像 EF 一样同时使用 xproj 和 csproj,或者 (2) 从 csproj 引用 NuGet 包.

Unfortunately, that's not possible, yet. You can only reference csproj from xproj, not the other way around. You have two alternatives: (1) have both xproj and csproj like EF does or (2) reference the NuGet package from csproj.

如果您想做替代 (2),那么您可以将 xproj 的输出设置到一个文件夹并将其添加为 NuGet 提要.

If you want to do alternative (2) then you can set the output of the xproj to a folder and add that as a NuGet feed.

这篇关于在单个解决方案中,在 DNX/ASP.NET 5 项目 (project.json/xproj) 和其他 C# 项目 (csproj) 之间共享代码的选项有哪些?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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