Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Api Analyzer and Windows compatibility package to write Intelligent Cross-platform .NET Core applications

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to use Api Analyzer and Windows compatibility package to write intelligent cross-platform .NET Core applications, the content is concise and easy to understand, can definitely make your eyes bright, through the detailed introduction of this article, I hope you can get something.

Text:

This is a pair of excellent tools in the .net Core world that you should know in recent weeks. They are very helpful when we write or migrate cross-platform code.

.net API Analyzer

The first is the API analyzer. As you know, sometimes we encounter outdated API, or a method that you can use in Windows but doesn't work in Linux. The API parser is a Roslyn (Roslyn is the name of the C#/.NET compiler) parser that can be easily added to your project through a NuGet package. All you have to do is add the package, and you will be immediately warned, or a wavy line indicates that there is something wrong with API.

Take a look at this simple example. I will generate a simple console application that has added an API parser. Note that this version number is current and will be changed later.

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

Then I'll use an API that can only be used under Windows. But I still want to run on any platform.

Static void Main (string [] args)

{

Console.WriteLine ("Hello World!")

If (RuntimeInformation.IsOSPlatform (OSPlatform.Windows))

{

Var w = Console.WindowWidth

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

}

}

Then I use the "dotnet build" command (or run the command, which also includes the build action) and I get a nice warning that this API doesn't work on all platforms.

C:\ supercrossplatapp > dotnet build

Program.cs: warning PC001: Console.WindowWidth 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 of the .NET team made a good YouTube video that shows how the API parser works. The code for the API parser has been uploaded to Github. If you find bug, please mention issue on it!

Windows compatibility Pack for .NET CORE

The second,. NET Core's Windows compatibility pack is a good complementary technology. When .NET Core 2.0 is developed and rolled out, it contains 32000 API that are well compatible with existing .NET Framework code. In fact, they are so compatible that I could easily port a 15-year-old .NET program to the .NET Core2.0 without any trouble.

From 13000 api in .NET Standard1.6 to 32000 API in .NET Standard2.0, they have more than doubled the number of available API

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

.NET Framework 4.6.1

.NET Core 2.0

Mono 5.4

Xamarin.iOS 10.14

Xamarin.Mac 3.8

Xamarin.Android 7.5

When you migrate code to the .NET Core, there will be a lot of Windows platform-specific dependencies, and you may encounter situations where API can't be found in the .NET Standard. At this point, the new NuGet package Microsoft.Windows.Compatibility (preview) provides API that was previously only available in the .NET Framework.

The API in this compatibility package will contain two types. One is that the API was originally part of Windows, but can also work across platforms. The other is that they can only work under windows because they are specific to the operating system. For example, API accesses the WIndows registry, which is unique under Windows. But System.DirectoryServices or System.Drawing can work well on any platform. The Windows compatibility Pack adds more than 20000 API to the Api now available in the .NET Core. You can see the video recorded by Immol.

The problem is, if these unavailable API get in the way of using the .NET Core, then you can use compatibility packs now, yay! But you need to know why you need to migrate to the .NET Core. Both the .NET Core and the full version of the .NET Framework work on Windows. If your program is currently working well and doesn't need the new features of the .NET Core, you don't need to migrate to the .NET Core. This is a list of rules:

When using the .NET Core:

You have cross-platform requirements.

Your goal is micro-service.

You use the Dorcker container

You have high performance and scalable needs

You need to synchronize the .NET version

When using the .NET Core:

Your program currently uses .NET Framework (extension is recommended instead of migration)

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

The. Net technology you are using is not supported on. NET Core

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

The above is how to use Api Analyzer and Windows compatibility package to write intelligent cross-platform .NET Core applications. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report