c# - .NET Core + .NET 4 在进程中并行执行?

标签 c# .net-core asp.net-core-2.1 side-by-side

在 Windows 上运行,在 .NET 4 的进程并行执行中是否支持 .NET Core 进程,如下所述:https://docs.microsoft.com/en-us/dotnet/framework/deployment/in-process-side-by-side-execution

谢谢

最佳答案

如果您根据引用的文档寻求“并排”:

Installing a new version of the .NET Framework has no effect on existing applications.

Applications run against the version of the .NET Framework that they were built with. They do not use the new version of the .NET Framework unless expressly directed to do so. However, it is easier for applications to transition to using a new version of the .NET Framework.


那么答案是 .
.NET Core 是一个独立的框架,不是 .NET Framework 的升级。因此,由于上述原因,在一个进程中并排运行两个 DLL 是无关紧要的。

Is there a way to host one .NET Framework DLL and one .NET Core DLL in the same process?


是的。
我见过两种可能的方式:
w3wp.exe
使用时 in-process hosting , it is possible to load both CLR and CoreCLR into w3wp.exe ,但我不知道这是如何工作的。它应该是托管代码世界之外的低级别/运行时级别的东西。
DLL 引用
.NET Core 应用程序可以直接引用 .NET Framework 库,只要定位 .NET Framework version implements .NET Standard .例如NET45+。
但是,.NET Framework 代码在 .NET Core shell 中的表现可能与在 .NET Framework shell 中不同。
考虑这个代码:
// In .NET Framework library
public class Class1
{
    public void Test()
    {
        var domain = AppDomain.CreateDomain("mydomain");
        Console.WriteLine(domain);
    }
}
如果您在 .NET Core 应用程序中引用此类型,您可能会看到 warning但没有编译时错误:
static void Main(string[] args)
{
    var class1 = new Class1();
    class1.Test();
}
当你运行代码时,你会得到一个异常:
System.PlatformNotSupportedException: 'Secondary AppDomains are not supported on this platform.'
相反,当您在 .NET Framework shell 中运行代码时,您会看到正常的输出:
Name:mydomain
There are no context policies.
为什么?
这是因为 AppDomain类型转发到不同的运行时库。
如果打印出类型所在的程序集:
Console.WriteLine(typeof(AppDomain).Assembly);
你可以看到 .NET Core 的输出是:
System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
而对于 .NET Framework,输出为:
mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

what kind of communication can I have between the two?

can I send serialized objects for example? Something similar on how two AppDomains can communicate?


根据 this docs 、.NET Core 不支持应用程序域和远程处理,因此您无法基于这些技术进行跨域通信。
如果你看看 MarshalByRefObject 的实现在 .NET Core 中,它只抛出异常。
但是,基于网络的通信(例如 gRPC )仍然是可能的。

关于c# - .NET Core + .NET 4 在进程中并行执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60424940/

相关文章:

c# - 获取可在其具体类型中类型转换的相关实体

c# - 指定的填充模式对此算法无效 - .net Core

c# - 如何使用 Visual Studio Code 在 dotnet 构建中包含资源文件

docker - Docker中带有ASP.NET Core 2.1的"It was not possible to find any compatible framework version"

identityserver4 - 调用/connect/userinfo返回错误 "Unhandled exception: Sequence contains more than one matching element"

asp.net-core - 如何使用 EFCore Code First Migrations 指定复合主键

c# - WebApi 在过滤器中获取帖子原始正文

c# - 具有数据库文件相对路径的连接字符串

c# - 是否可以从另一个类库中的静态类获取 ConnectionString?

.net-core - 如何使 dotnet publish 命令使用私有(private) Nuget 源