官术网_书友最值得收藏!

Dependencies and frameworks

Inside a .NET Core project, you specify the frameworks that you wish to target. What are these frameworks? Well, .NET Core itself, but the classic .NET Framework as well, Xamarin, Universal Windows Platform (UWP), Portable Class Libraries (PCL), Mono, Windows Phone, and more.

In the early days of .NET Core, you would either target .NET Core itself, or/as well as one of these other frameworks. Now it is advisable to target standards instead. Now we have .NET Standard, and the differences between the two are as follows:

  • .NET Standard is a specification (a contract) that covers which APIs a .NET platform has to implement.
  • .NET Core is a concrete .NET platform and implements the .NET Standard.
  • The latest .NET Standard will always cover the highest .NET full framework released.

David Fowler (https://twitter.com/davidfowl) of Microsoft came up with the following analogy:

interface INetStandard10
{
void Primitives();
void Reflection();
void Tasks();
void Collections();
void Linq();
}


interface INetStandard11 : INetStandard10
{
void ConcurrentCollections();
void InteropServices();
}

interface INetFramework45 : INetStandard11
{
// Platform specific APIs
void AppDomain();
void Xml();
void Drawing();
void SystemWeb();
void WPF();
void WindowsForms();
void WCF();
}

This should make it very easy to understand. As you can see, all .NET APIs that need Windows (WPF, Windows Forms, Drawing) are only available in a specific platform (.NET 4.5), not a standard. Standards are for cross-platform functionality.

So instead of targeting a specific version, such as .NET 4.5.1, .NET Core 1.0, Mono, Universal Windows Platform 10, you should target a .NET Standard. Your project is guaranteed to work on all platforms that support that standard (or a higher one), either existing or waiting to be created. You should try to keep your dependency to the lowest standard possible to increase the number of platforms that your app will work on, if that is important to you.

The current mapping between the different .NET frameworks and the .NET Standard they implement at the time this book was written is always available at https://github.com/dotnet/standard/blob/master/docs/versions.md.
.NET Core 2.0 and .NET Standard 2.0 were made available in August 2017, and now four frameworks target .NET Standard 2.0:

  • .NET Framework full
  • .NET Core 2.x
  • Xamarin
  • Mono

.NET Core 3.0 was made available on September 2019 and with it .NET Standard 2.1.

You can have your dependencies specified per target or for all targets. In the former case, all of the dependencies need to support all of the targets, and in the latter, we can have different dependencies for each target. You'll probably want a mix of the two, with common libraries as global dependencies and more specialized libraries specified only where available. If you target more than one standard (or framework), then pay attention, because you may have to resort to conditional definitions (#if) to target those features that only exist in one of them. Let's see how.

The .NET Standard FAQ is available in GitHub at https://github.com/dotnet/standard/blob/master/docs/faq.md.

Targeting .NET Core or the full .NET framework

It is important that you know that you can target the full .NET framework in an ASP.NET Core application! However, if you do this, you will lose the platform independence—that is, you will only be able to run it on Windows.

By default, an ASP.NET Core project targets netcoreapp1.x, netcoreapp2.x, or netcoreapp3.x, depending on whether you are targeting ASP.NET Core 1.x, 2.x, or 3.x, but you can change it in the .csproj file. If you just want to target one framework, then modify the TargetFramework element like this:

<TargetFramework>net461</TargetFramework>

Or, if you want to target more than one, replace TargetFrameworkwith TargetFrameworks :

<TargetFrameworks>netcoreapp3.0;net461</TargetFrameworks>

For more information, please refer to the Microsoft documentation at https://docs.microsoft.com/en-us/dotnet/core/tools/csproj.

For .NET Core and .NET Standard, you should use the following names in TargetFramework or TargetFrameworks:

Please see https://docs.microsoft.com/en-us/dotnet/standard/frameworks for the up-to-date list. Next, let's see how generic hosting works.

主站蜘蛛池模板: 济宁市| 五华县| 固镇县| 巴青县| 宣武区| 鄢陵县| 桐庐县| 沽源县| 稷山县| 社会| 察隅县| 九江县| 隆化县| 长阳| 綦江县| 黄山市| 水城县| 木里| 石林| 青州市| 大关县| 阳谷县| 华池县| 利辛县| 噶尔县| 澎湖县| 崇仁县| 化州市| 临泽县| 伊春市| 会同县| 琼结县| 堆龙德庆县| 舒城县| 新丰县| 筠连县| 日喀则市| 阿克陶县| 淮南市| 平湖市| 福鼎市|