dotnet官方在github上给出了嵌入dotnet-core的方式
https://github.com/dotnet/samples
具体位置如下:
https://github.com/dotnet/samples/tree/main/core/hosting
Sample .NET Core Hosts
This folder contains sample code demonstrating how to host managed .NET Core code in a native process. These hosts bypass the usual dotnet
host and launch managed code directly.
There are two samples demonstrating two different hosting interfaces for .NET Core.
- The HostWithHostFxr folder demonstrates how to host the .NET Core runtime using the
nethost
andhostfxr
libraries’ APIs. These APIs were introduced in .NET Core 3.0 and are the recommended method of hosting .NET Core 3.0 and above. These entry points handle the complexity of finding and setting up the runtime for initialization. This host demonstrates calling from native code into a static managed method and passing it a message to display. - The HostWithCoreClrHost folder demonstrates how to host the .NET Core runtime using the newer
coreclrhost.h
API. This API is the preferred method of hosting .NET Core 2.2 and below. This host demonstrates calling from native code into a static managed method and supplying a function pointer for the managed code to use to call back into the host.
These hosts are small and bypass a lot of complexity (probing for assemblies in multiple locations, thorough error checking, etc.) that a real host would have. Hopefully by remaining simple, though, they will be useful for demonstrating the core concepts of hosting managed .NET Core code in a native process. Other (more real-world) hosts which may be useful as a guide can be found in .NET Core product source in the dotnet/runtime repository.
These samples are part of the .NET Core hosting tutorial. Please see that topic for a more detailed explanation of the samples and the steps necessary to host .NET Core.
从介绍中我们可以看到dotnet Core2.2以前我们应该参考HostWithCoreClrHost,而3.0以后我们则应该使用HostWithHostFxr。
后续我们会使用这个demo来编写我们自己的嵌入.net core虚拟机。