class 库项目中的目标框架 dnx451 或 net451

Target framework dnx451 or net451 in class library projects

据我了解,目标框架 dnx451net451 都使用桌面 .NET Framework 4.5.1。 dnx451 专为 DNX 运行时应用而设计,支持 ASP.NET 5.

如果我们有一个包含 ASP.NET 5 项目和多个 class 库的解决方案,它们应该都以 dnx451 为目标还是只有 Web 项目需要以 dnx451 为目标]? class 库可以只针对 net451 吗?

更新

根据 https://github.com/aspnet/Announcements/issues/98 命名稍有变化。

For RC1 applications and test projects should still target dnx4x and dnxcore50 and don't need to change at all. Anything that has a Program.Main or Startup.cs is considered an application.

Only class libraries should change to target net4x and dotnet5.x. For class libraries the recommended conversion steps are:

In project.json:

Change dnx4x to net4x (e.g. dnx451 to net451)

Change dnxcore50 to dotnet5.4

And in your CS files:

Change #if DNX451 to #if NET451

Change #if DNXCORE50 to #if DOTNET5_4


如果您有一个针对多个框架的项目并且想要添加对库的一般引用,则该库必须支持所有这些框架。如果你指定某个框架本身引用那个库,那么那个库只需要支持选择的框架。

示例,如果我想为我的所有框架引用 library1:

"dependencies": {
    "library1": "1.0.0"
},

"frameworks": {
    "dnx451": { },
    "dnxcore50": { }
}

那么library1必须支持dnx451和dnxcore50

如果我想引用 library1 但它只支持 dnx451 那么这是我唯一的选择:

"dependencies": {
},

"frameworks": {
    "dnx451": {
        "dependencies": {
           "library1": "1.0.0"
        }
     },
    "dnxcore50": { } 
}

但这意味着 library1 代码无法在 dnx451 中使用。

要解决这个问题,您可以使用编译时条件:

#if DNX451
    //Code here for dnx451
#elif DNXCORE50
    //code here for dnxcore50
#endif

或者另一种变通方法是为另一个依赖项使用另一个库

澄清一下,该库可以支持比您的项目更多的框架。所以 library1 可以支持 dnx451 和 dnxcore50 而你的项目只支持 dnx451 就可以了

您的库必须支持您的 Web 项目目标的所有框架。这就是为什么 VS15 中默认的 Class 库项目以 dotnet 为目标,它支持最广泛的框架。

如果您的 class 库不依赖于 .Net SDK 的特定行为(例如 DNX 或完整的 .Net Framework),您应该以 dotnet 为目标。但这只适用于较新的 SDK(.Net 4.6、UWP 和 DNX),

http://oren.codes/2015/06/09/pcls-net-core-dnx-and-uwp/

名字 dnx451 或 net451 本质上是一样的。

它们在某个时候被重新命名,因此,dnxcore50 变成了 dotnet5.4dnx451 变成了 net451

供参考:https://github.com/aspnet/Announcements/issues/98