Shimakaze.NetHost is a set of managed wrapper libraries for the .NET Hosting API, enabling you to safely call the native nethost and hostfxr interfaces from C#. It lets you start and control the .NET runtime from native code, plugin systems, script hosts, or NativeAOT applications.
- Project Overview
- Package Structure
- Features
- Target Frameworks
- Quick Start
- API Overview
- NativeAOT Support
- Build and Package
- References
- License
The .NET Hosting API allows native code to locate, load, and start the .NET runtime. It is commonly used for:
- Embedding the .NET runtime into a C/C++ native host application.
- Writing plugin hosts that can load managed assemblies.
- Invoking managed code from a NativeAOT application via
hostfxr.
The .NET official documentation provides C/C++ examples. Shimakaze.NetHost wraps these native interfaces into an easy-to-use C# API and includes the native object files required for NativeAOT static linking, saving you from manually configuring P/Invoke and MSBuild.
| Package | Path | Primary Responsibility |
|---|---|---|
Shimakaze.NetHost |
src/Shimakaze.NetHost |
Wraps nethost's get_hostfxr_path to locate the hostfxr library. Includes NetHost.targets to automatically resolve NativeAOT static-linking assets. |
Shimakaze.HostFxr |
src/Shimakaze.HostFxr |
Wraps all core hostfxr functions: runtime initialization, assembly loading, delegate retrieval, runtime property access, environment queries, and more. |
- Cross-platform: Automatically detects Windows, Linux, and macOS, handling Unicode (Windows) and UTF-8 (Unix) string encodings.
- Broad target framework support: Compatible from
.NET 10down to.NET 2.0/.NET Standard 1.1. - NativeAOT compatible: Targets for .NET 8.0+ are marked as
IsAotCompatible. - Automatic native asset resolution:
NetHost.targetsautomatically locateslibnethoststatic and dynamic libraries and statically links them when publishing NativeAOT. - Safe wrapper: Uses
IDisposable; unsafe pointers are kept internal, exposing only managed strings and arrays to consumers. - Zero runtime dependencies: The wrapper libraries themselves do not depend on any additional NuGet packages.
Specified centrally in Directory.Build.props:
.NET 10/.NET 9/.NET 8/.NET 7/.NET 6/.NET 5.NET Core 3.1/3.0/2.2/2.1/2.0/1.1/1.0.NET Framework 4.8.1down to4.0, plus3.5/2.0.NET Standard 2.1/2.0/1.6/1.5/1.4/1.3/1.2/1.1
dotnet add package Shimakaze.NetHost
dotnet add package Shimakaze.HostFxrusing Shimakaze;
// Get the path to the hostfxr library (provided by nethost)
string hostfxrPath = NetHost.GetHostFxrPath();
// Load hostfxr
using var hostfxr = new HostFXR(hostfxrPath);// Initialize the runtime via runtimeconfig.json
var parameters = new InitializeParameters
{
HostPath = "MyApp.exe",
DotnetRoot = @"C:\Program Files\dotnet"
};
hostfxr.InitializeForRuntimeConfig(
"MyApp.runtimeconfig.json",
parameters,
out HostFXRHandle context);
// Load an assembly and retrieve the entry-point delegate
context.LoadAssemblyAndGetFunctionPointer(
"MyAssembly.dll",
"MyNamespace.MyClass",
"MyMethod",
null,
out nint delegatePtr);
// Close the context
context.Close();hostfxr.InitializeForDotnetCommandLine(
new[] { "dotnet", "MyApp.dll" },
parameters,
out HostFXRHandle context);
int exitCode = context.RunApp();
context.Close();| Method | Description |
|---|---|
NetHost.GetHostFxrPath() |
Calls nethost.get_hostfxr_path and returns the full path to the hostfxr library. |
| Method | Description |
|---|---|
Main / MainStartupinfo / MainBundleStartupinfo |
Directly invoke the hostfxr_main family of entry points. |
InitializeForDotnetCommandLine |
Parse command-line arguments and initialize a runtime context. |
InitializeForRuntimeConfig |
Initialize a runtime context via runtimeconfig.json. |
SetErrorWriter / GetDotnetEnvironmentInfo |
Set an error writer / enumerate installed SDKs and frameworks. |
GetRuntimePropertyValue / SetRuntimePropertyValue / GetRuntimeProperties |
Read and write runtime properties. |
RunApp |
Run the .NET application associated with the current context. |
Close |
Close the hostfxr context. |
Dispose |
Release the hostfxr library handle. |
| Method | Description |
|---|---|
LoadAssemblyAndGetFunctionPointer |
Load an assembly and retrieve a method delegate for a type. |
GetFunctionPointer |
Retrieve a method pointer for a type in a loaded assembly. |
LoadAssembly |
Load an assembly into the runtime context. |
LoadAssemblyBytes |
Load an assembly from in-memory bytes (optional PDB). |
GetRuntimePropertyValue / SetRuntimePropertyValue / GetRuntimeProperties |
Read and write context properties. |
RunApp |
Run the context application. |
Close / Dispose |
Close the context. |
Corresponds to hostfxr_delegate_type, including:
LoadAssemblyAndGetFunctionPointerGetFunctionPointerLoadAssemblyLoadAssemblyBytesComActivationWinrtActivationComRegister/ComUnregisterLoadInMemoryAssembly
Shimakaze.NetHost ships with NetHost.targets in the NuGet package, which automatically:
- Resolves the current Runtime Identifier (
RuntimeIdentifier/DefaultAppHostRuntimeIdentifier/NETCoreSdkRuntimeIdentifier). - Locates
Microsoft.NETCore.App.Host.<RID>from the SDK targeting pack or the NuGet global packages directory. - Passes
libnethost.a/libnethost.libasNativeLibraryto the NativeAOT linker. - Declares
DirectPInvokefornethostso that P/Invoke can be statically resolved. - Appends the
-lstdc++linker argument on Linux and macOS. - Copies the dynamic library
nethost.dll/libnethost.so/libnethost.dylibto the output directory for non-AOT runtime use.
When using
Shimakaze.NetHost, you do not need to manually download or configurenethostnative libraries.
This project uses .NET SDK 10 + GitVersion.MsBuild 6.x for versioning, with Central Package Management enabled.
# Restore
dotnet restore --graph --artifacts-path artifacts
# Build Release
dotnet build --graph --artifacts-path artifacts --configuration Release --no-restore
# Pack (includes symbols package)
dotnet pack --graph --artifacts-path artifacts --configuration Release --no-restore --no-build --include-symbolsThe CI/CD configuration is located at .github/workflows/build.yaml:
- Builds are triggered on push and pull_request to the
masterbranch. - GitHub Releases are created and NuGet packages are pushed on
v*tags.
- Write a custom .NET host to control the .NET runtime from your native code
- Native code interop with Native AOT
This project is licensed under the MIT License.
Copyright © 2025 frg2089 frg2089@outlook.com