.net core compatibility windows & windows compatible Linux
Who is this package for?
This package is meant for developers that need to port existing .NET Framework code to .NET Core. But before you start porting, you should understand what you want to accomplish with the migration. Just porting to .NET Core because it’s a new .NET implementation isn’t a good enough reason (unless you’re a True Fan).
.NET Core is optimized for building highly scalable web applications, running on Windows, macOS or Linux. If you’re building Windows desktop applications, then the .NET Framework is the best choice for you. Take a look at our documentation for more details on how to choose between .NET Core and .NET Framework.
Demo
For a demo, take a look at this video:
Using the Windows Compatibility Pack
We highly recommend that you plan your migrations as a series of steps instead of assuming you can port the existing code base all at once. If you’re planning to migrate an ASP.NET MVC application running on a local Windows server to an ASP.NET Core application running on Linux in Azure, we’d recommend you perform these steps:
- Migrate to ASP.NET Core (while still targeting the .NET Framework)
- Migrate to .NET Core (while staying on Windows)
- Migrate to Linux
- Migrate to Azure
The order of steps might vary, depending on your business goals and what value you need to accomplish first. For example, you might need to deploy to Azure before you perform the other migration steps. The primary point is that you perform one step at a time to ensure your application stays operational along the way. This reduces the complexity and churn you have to reason about at once. It also allows you to learn more about your code base and adjust your plans as you discover issues.
The Porting to .NET Core from .NET Framework documentation provides more details on the recommended process and which tools you can use.
Before bringing existing .NET Framework code to a .NET Core project, we recommend you first add the Windows Compatibility Pack by installing the NuGet package Microsoft.Windows.Compatibility. This maximizes the number of APIs you have at your disposal.
The Windows Compatibility Pack is currently in preview because it’s still a work in progress. The following table describes the APIs that are already part of the Windows Compatibility Pack or are coming in a subsequent updat
| Component | Status | Windows-Only | Component | Status | Windows-Only |
|---|---|---|---|---|---|
| Microsoft.Win32.Registry | Available | Yes | System.Management | Coming | Yes |
| Microsoft.Win32.Registry.AccessControl | Available | Yes | System.Runtime.Caching | Coming | |
| System.CodeDom | Available | System.Security.AccessControl | Available | Yes | |
| System.ComponentModel.Composition | Coming | System.Security.Cryptography.Cng | Available | Yes | |
| System.Configuration.ConfigurationManager | Available | System.Security.Cryptography.Pkcs | Available | Yes | |
| System.Data.DatasetExtensions | Coming | System.Security.Cryptography.ProtectedData | Available | Yes | |
| System.Data.Odbc | Coming | System.Security.Cryptography.Xml | Available | Yes | |
| System.Data.SqlClient | Available | System.Security.Permissions | Available | ||
| System.Diagnostics.EventLog | Coming | Yes | System.Security.Principal.Windows | Available | Yes |
| System.Diagnostics.PerformanceCounter | Coming | Yes | System.ServiceModel.Duplex | Available | |
| System.DirectoryServices | Coming | Yes | System.ServiceModel.Http | Available | |
| System.DirectoryServices.AccountManagement | Coming | Yes | System.ServiceModel.NetTcp | Available | |
| System.DirectoryServices.Protocols | Coming | System.ServiceModel.Primitives | Available | ||
| System.Drawing | Coming | System.ServiceModel.Security | Available | ||
| System.Drawing.Common | Available | System.ServiceModel.Syndication | Coming | ||
| System.IO.FileSystem.AccessControl | Available | Yes | System.ServiceProcess.ServiceBase | Coming | Yes |
| System.IO.Packaging | Available | System.ServiceProcess.ServiceController | Available | Yes | |
| System.IO.Pipes.AccessControl | Available | Yes | System.Text.Encoding.CodePages | Available | Yes |
| System.IO.Ports | Available | Yes | System.Threading.AccessControl | Available | Yes |
Handling Windows-only APIs
If you plan to run your .NET Core application on Windows only, then you don’t have to worry about whether an API is cross-platform or not. However, if you plan to migrate your application to Linux or macOS, you need to take the platform support into account.
As you can see in the previous table, about half of the components in the Windows Compatibility Pack are Windows-only, the other half works on any platform. Your code can always assume that all the APIs exist across all platforms, but if they are Windows-only they throw PlatformNotSupportedException. This allows you to write code that calls Windows-only APIs after doing a platform check at runtime, rather than having to use conditional compilation using #if. We recommend you to use RuntimeInformation.IsOSPlatform() for platform checks:
private static string GetLoggingPath()
{
// Verify the code is running on Windows.
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
using (var key = Registry.CurrentUser.OpenSubKey(@"Software\Fabrikam\AssetManagement"))
{
if (key?.GetValue("LoggingDirectoryPath") is string configuredPath)
return configuredPath;
}
} // This is either not running on Windows or no logging path was configured,
// so just use the path for non-roaming user-specific data files.
var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
return Path.Combine(appDataPath, "Fabrikam", "AssetManagement", "Logging");
}
You might wonder how you’re supposed to know which APIs are Windows-only. The obvious answer would be documentation, but that’s not very convenient. This is one of the reasons why we introduced the API Analyzer tool two weeks ago. It’s a Roslyn-based analyzer that will flag usages of Windows-only APIs when you’re targeting .NET Core and .NET Standard. For the previous sample, this looks as follows:

You have three options to deal with Windows-only API usages:
- Remove. Sometimes you might get away with simply deleting the code as you don’t plan to migrate certain features to the .NET Core-based version of your application.
- Replace. Usually, you’ll want to preserve the general feature so you might have to replace the technology with one that is cross-platform. For example, instead of saving configuration state in the registry, you’d use text-based configuration files you can read from all platforms.
- Guard. In some cases, you may want to call the Windows-only API when you’re running on Windows and simply do nothing (or call a Linux-specific API) when you’re running on Linux.
In the previous example, the code is already written in such a way that it provides a default configuration when the setting isn’t found in the registry. So the easiest solution is to guard the call to registry APIs behind a platform check.
The Windows Compatibility Pack is designed as a metapackage, meaning it doesn’t directly contain any libraries but references other packages. This allows you to quickly bring in all the technologies without having to hunt down various packages. But as your port progresses, you may find it useful to reference individual packages instead. This allows you to remove dependencies and ensure newly written code in that project doesn’t take a dependency on it again.
Summary
When you port existing code from the .NET Framework to .NET Core, install the new Windows Compatibility Pack. It provides access to an additional 20,000 APIs, compared to what is available in .NET Core. This includes drawing, EventLog, WMI, Performance Counters, and Windows Services.
If you plan to make your code cross-platform, use the new API Analyzer to ensure you don’t accidentally depend on Windows-only APIs.
But remember that the .NET Framework is still the best choice for building desktop applications as well as Web Form-based web applications. If you’re happy on the .NET Framework, there is also no reason to port to .NET Core.
Let us know what you think!
.net core compatibility windows & windows compatible Linux的更多相关文章
- Running ASP.NET Core applications on Windows Subsystem for Linux
Setting up Linux on Windows 10 First thing is to enable Windows Subsystem for Linux. It doesn’t inst ...
- NET Core应用可以同时运行在Windows Container和Linux Container-1
NET Core多平台开发体验[1]: Windows 微软在千禧年推出 .NET战略,并在两年后推出第一个版本的.NET Framework和IDE(Visual Studio.NET 2002,后 ...
- .NET CORE QuartzJob定时任务+Windows/Linux部署
前言 以前总结过一篇基于Quartz+Topshelf+.netcore实现定时任务Windows服务 https://www.cnblogs.com/gt1987/p/11806053.html.回 ...
- 如何实现在Windows上运行Linux程序,附示例代码
微软在去年发布了Bash On Windows, 这项技术允许在Windows上运行Linux程序, 我相信已经有很多文章解释过Bash On Windows的原理, 而今天的这篇文章将会讲解如何自己 ...
- WSL(Windows Subsystem for Linux)--Pico Process Overview
[转载] Windows Subsystem for Linux -- Pico Process Overview Overview This post discusses pico processe ...
- Windows远程连接Linux
目录 xrdp方式 vnc方式 xrdp方式 ----------------------------------------------------------------------------- ...
- 使用NSSM把.Net Core部署至 Windows 服务
为什么部署至Windows Services 在很多情况下,很少会把.Net Core项目部署至Windows服务中,特别是Asp.net Core就更少了.一般情况下,Asp.net Core会部署 ...
- NSSM把.Net Core部署至 Windows 服务
NSSM把.Net Core部署至 Windows 服务 https://www.cnblogs.com/emrys5/p/nssm-netcore.html 为什么部署至Windows Servic ...
- Windows软件在Linux上的等价/替代/模仿软件列表 (抄一个)
Last update: 16.07.2003, 31.01.2005, 27.05.2005 您可在以下网站发现本列表最新版:http://www.linuxrsp.ru/win-lin-soft/ ...
随机推荐
- 理解使用before,after伪类实现小三角形气泡框
先来理解before和after伪类的用法吧,before从字面上的意思可以理解为前面的意思,它一般和content属性一起使用,把内容插入在其他元素的前面,同理after的含义就是把内容插入到其他元 ...
- 【P1941】 飞扬的小鸟
题目描述 游戏界面是一个长为 nn,高为 mm 的二维平面,其中有 kk 个管道(忽略管道的宽度). 小鸟始终在游戏界面内移动.小鸟从游戏界面最左边任意整数高度位置出发,到达游戏界面最右边时,游戏完成 ...
- (5)学习笔记 ) ASP.NET CORE微服务 Micro-Service ---- 熔断降级(Polly)
一. 什么是熔断降级 熔断就是“保险丝”.当出现某些状况时,切断服务,从而防止应用程序不断地尝试执行可能会失败的操作给系统造成“雪崩”,或者大量的超时等待导致系统卡死. 降级的目的是当某个服务提供者发 ...
- 朱晔的互联网架构实践心得S1E10:数据的权衡和折腾【系列完】
朱晔的互联网架构实践心得S1E10:数据的权衡和折腾[系列完] [下载本文PDF进行阅读] 本文站在数据的维度谈一下在架构设计中的一些方案对数据的权衡以及数据流转过程中的折腾这两个事情.最后进行系列文 ...
- POST BOY : Restful API 调试工具
在使用asp.net webapi开发中,一般情况下会使用一些工具来模拟请求. 其中有一款chrome浏览器插件POST MAN比较不错. 但是由于国内google服务使用不稳定,所以我自己写了一个简 ...
- xadmin集成DjangoUeditor
1.安装 安装DjangoUeditor 1)去GitHub上面下载djangoueditor源码包(https://github.com/twz915/DjangoUeditor3) 然后进入源 ...
- 七、xadmin 编辑界面实现二级联动
很多时候,我们会遇到这种需求,通过一个select框中选择的值,去动态的加载另一个下拉框中的内容 对于前端的同学来讲,这个本应该是一个很简单的需求,获取第一个下拉框的值然后通过ajax去动态加载即可. ...
- JS 有趣的JS
一. var arr = []; for (var i = 0; i < 3; i++) { arr[i] = function() { console.log(i+'__') // 3 3 3 ...
- 自定义threading.local
1.threading相关. # Author:Jesi # Time : 2018/12/28 14:21 import threading import time from threading i ...
- C\C++学习笔记 2
C++记录4 自动存储: 生命周期在代码块,存储在栈,后入先出. 静态存储: 存在于程序的整个周期. 动态存储: 使用new delete 在内存池(堆)存储,不受程序生命周期控制. 内存泄露: 没有 ...