为什么 Microsoft 将 AppDomain 带回 .NET Core 3.0?

Why did Microsoft bring AppDomain back to .NET Core 3.0?

记忆中,微软拿走了AppDomain,这个机制已经关闭

现在突然发现AppDomain回来了:

Assembly System.Runtime.Extensions, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a\dotnet\packs\Microsoft.NETCore.App.Ref.0.0\ref\netcoreapp3.0\System.Runtime.Extensions.dll

程序集包含 AppDomain class。

这是我的问题:为什么?

Porting to .NET Core

Why was it discontinued? AppDomains require runtime support and are generally quite expensive. While still implemented by CoreCLR, it’s not available in .NET Native and we don’t plan on adding this capability there.

What should I use instead? AppDomains were used for different purposes. For code isolation, we recommend processes and/or containers. For dynamic loading of assemblies, we recommend the new AssemblyLoadContext class.

此外

Of course, just because something isn’t available in .NET Core today doesn’t mean we discontinued it. In most cases, it simply means we haven’t had the time to investigate whether porting would make sense or didn’t think it was relevant to the application models .NET Core currently offers. Thus, this is an area we’re highly interested in getting your feedback.

那么实际支持的是什么?

AppDomain Class

On .NET Core, the AppDomain implementation is limited by design and does not provide isolation, unloading, or security boundaries. For .NET Core, there is exactly one AppDomain. Isolation and unloading are provided through AssemblyLoadContext. Security boundaries should be provided by process boundaries and appropriate remoting techniques.

It's there for certain tasks, however it's not supported in a lot of ways.

适用于

.NET Core

  • 3.0 2.2 2.1 2.0

.NET Framework

  • 4.8 4.7.2 4.7.1 4.7 4.6.2 4.6.1 4.6 4.5.2 4.5.1 4.5 4.0 3.5 3.0 2.0 1.1

.NET Standard

  • 2.1 2.0

那么如果您使用不受支持的东西会怎样?

它要么不存在,要么你会得到一个明显的令人讨厌的 throw new NotSupportedExceptionPlatformNotSupportedException,具体取决于何时何地

进一步阅读