Use the Api analyzer and the Windows compatibility package to write smart cross-platform. NET Core applications. The api Analyzer

Source: Internet
Author: User

Use the Api analyzer and the Windows compatibility package to write smart cross-platform. NET Core applications. The api Analyzer

This article is translated from Scott Hanselman's blog:

Https://www.hanselman.com/blog/WritingSmarterCrossplatformNETCoreAppsWithTheAPIAnalyzerAndWindowsCompatibilityPack.aspx

Body:

This is a pair of excellent. Net Core tools you should know in recent weeks. They are very helpful when writing or porting cross-platform code.


. Net api Analyzer

The first is the API analyzer. As you know, sometimes we encounter outdated APIs or methods that you can use in Windows, but cannot work in Linux. API analyzer is a Roslyn (Roslyn is the name of the C #/. NET compiler) analyzer that can be easily added to your project through a NuGet package. All you need to do is add this package, and you will be immediately warned, or the ripple line indicates that the API is faulty.

Let's take a look at this simple example. I will generate a simple console application that has added an API analyzer. Note that the current version number will change later.

C:\supercrossplatapp> dotnet new consoleC:\supercrossplatapp> dotnet add package Microsoft.DotNet.Analyzers.Compatibility --version 0.1.2-alpha

Then I will use an API that can only be used in Windows. But I still want to run it on any platform.

Static void Main (string [] args)

{

Console. WriteLine ("Hello World! ");

 

If (RuntimeInformation. IsOSPlatform (OSPlatform. Windows ))

{

Var w = Console. cmdwwidth;

Console. WriteLine ($ "Console Width is {w }");

}

}

Then I run the "dotnet build" command (or run the command, which also contains the build action) and then I got a pretty warning that this API cannot work on all platforms.


C: \ supercrossplatapp> dotnet build


Program. cs (): warning PC001: Console. Unzip wwidth isn' t supported on Linux, MacOSX [C: \ Users \ scott \ Desktop \ supercr

Ossplatapp \ supercrossplatapp. csproj]

Supercrossplatapp-> C: \ supercrossplatapp \ bin \ Debug \ netcoreapp2.0 \ supercrossplatapp. dll


Build succeeded.

Olia from the. NET team made a good YouTube video that showed how the API analyzer works. The API analyzer code has been uploaded to Github. If you find a bug, please mention issue above!

. Net core windows compatibility package

Second, the. NET Core Windows compatibility package is a good complementary technology. After. NET Core 2.0 is developed and launched, it contains 32 thousand APIs that are well compatible with existing. NET Framework code. In fact, they are indeed very compatible. I was able to easily port A. NET program 15 years ago to. NET Core2.0 without any trouble.

From 13 thousand APIs of. NET Standard1.6 to 32 thousand APIs of. NET Standard2.0, they have more than doubled the number of available APIs.

. NET Standard is very cool because it supports the following platforms:

  • . NET Framework 4.6.1

  • . NET Core 2.0.

  • Mono 5.4

  • Xamarin. iOS 10.14

  • Xamarin. mac3.8

  • Xamarin. Android 7.5

When you migrate code to. NET Core, there will be a large number of Windows platform-specific dependencies, and you may encounter situations where some APIs cannot be found in. NET Standard. In this case, the new NuGet package, Microsoft. Windows. Compatibility (PreView), can provide APIs that are only available in. NET Framework.

The API in this compatibility package contains two types. One is that this API was originally part of Windows, but it can also work across platforms. The other is to work only in windows, because they are unique to the operating system. For example, API access to the WIndows registry is unique in Windows. However, System. DirectoryServices or System. Drawing can work well on any platform. The Windows compatibility package adds more than 20000 APIs based on the currently available APIs of. NET Core. You can refer to videos recorded by Immol.

The problem is that if these unavailable APIs are a roadblock for your use of. NET Core, now you can use compatible packages! But you need to know why you need to migrate to. NET Core .. Both NET Core and the full. NET Framework can work on Windows. If your program currently works well and does not require new. NET Core features, you do not need to port it to. NET Core. This is the rule list:

Usage of. NET Core:

  • You have cross-platform requirements

  • Your goal is microservice.

  • You use the Dorcker container

  • You have high performance and scalability requirements

  • You need to synchronize the. NET version

Usage of. NET Core:

  • Your program currently uses. NET Framework (expansion is recommended to replace migration)

  • Your program uses third-party. NET libraries or NuGet packages, and Their. NET Core versions are not available yet.

  • Your. NET technology is not supported on. NET Core.

  • The operating system platform used by your program is currently not supported by. NET Core.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.