Shimakaze.HostFxr 0.0.10

dotnet add package Shimakaze.HostFxr --version 0.0.10
                    
NuGet\Install-Package Shimakaze.HostFxr -Version 0.0.10
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Shimakaze.HostFxr" Version="0.0.10" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Shimakaze.HostFxr" Version="0.0.10" />
                    
Directory.Packages.props
<PackageReference Include="Shimakaze.HostFxr" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Shimakaze.HostFxr --version 0.0.10
                    
#r "nuget: Shimakaze.HostFxr, 0.0.10"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Shimakaze.HostFxr@0.0.10
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Shimakaze.HostFxr&version=0.0.10
                    
Install as a Cake Addin
#tool nuget:?package=Shimakaze.HostFxr&version=0.0.10
                    
Install as a Cake Tool

Shimakaze.NetHost

Shimakaze.NetHost 是一组用于 .NET Hosting API 的托管封装库,让你能够用 C# 安全地调用 nethosthostfxr 原生接口,从而在原生代码、插件系统、脚本宿主或 NativeAOT 应用中启动并控制 .NET 运行时。

Build License: MIT NuGet Shimakaze.NetHostNuGet Shimakaze.NetHost Downloads NuGet Shimakaze.HostFxrNuGet Shimakaze.HostFxr Downloads


目录


项目概述

.NET Hosting API 允许原生代码定位、加载并启动 .NET 运行时,常用于:

  • 把 .NET 运行时嵌入 C/C++ 原生宿主程序;
  • 编写可加载托管程序集的插件宿主;
  • 在 NativeAOT 应用中通过 hostfxr 调用托管代码。

.NET 官方文档 给出了 C/C++ 示例,而 Shimakaze.NetHost 将这些原生接口包装成易用的 C# API,并内置了 NativeAOT 静态链接所需的目标文件,省去手工配置 P/Invoke 与 MSBuild 的繁琐步骤。


包结构

包名 路径 主要职责
Shimakaze.NetHost src/Shimakaze.NetHost 封装 nethostget_hostfxr_path,用于定位 hostfxr 库。附带 NetHost.targets,自动解析 NativeAOT 静态链接资产。
Shimakaze.HostFxr src/Shimakaze.HostFxr 封装 hostfxr 全部核心函数:运行时初始化、加载程序集、获取委托、读取运行时属性、环境信息查询等。

功能特性

  • 跨平台:自动识别 Windows / Linux / macOS,处理 Unicode(Windows)与 UTF-8(Unix)字符串编码。
  • 极广的目标框架支持:从 .NET 10 一直向下兼容到 .NET 2.0 / .NET Standard 1.1
  • NativeAOT 兼容:.NET 8.0+ 目标标记为 IsAotCompatible
  • 自动原生资产解析NetHost.targets 自动查找 libnethost 静态库与动态库,并在发布 NativeAOT 时静态链接。
  • 安全封装:使用 IDisposable、不安全指针仅在内部使用,对外暴露托管字符串与数组。
  • 零运行时依赖:封装库本身不依赖任何额外 NuGet 包。

目标框架

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.14.0,以及 3.5 / 2.0
  • .NET Standard 2.1 / 2.0 / 1.6 / 1.5 / 1.4 / 1.3 / 1.2 / 1.1

快速开始

1. 安装 NuGet 包

dotnet add package Shimakaze.NetHost
dotnet add package Shimakaze.HostFxr

2. 定位 hostfxr 并加载

using Shimakaze;

// 获取 hostfxr 库路径(nethost 提供)
string hostfxrPath = NetHost.GetHostFxrPath();

// 加载 hostfxr
using var hostfxr = new HostFXR(hostfxrPath);

3. 初始化运行时并加载程序集

// 通过 runtimeconfig.json 初始化运行时
var parameters = new InitializeParameters
{
    HostPath = "MyApp.exe",
    DotnetRoot = @"C:\Program Files\dotnet"
};

hostfxr.InitializeForRuntimeConfig(
    "MyApp.runtimeconfig.json",
    parameters,
    out HostFXRHandle context);

// 加载程序集并获取入口委托
context.LoadAssemblyAndGetFunctionPointer(
    "MyAssembly.dll",
    "MyNamespace.MyClass",
    "MyMethod",
    null,
    out nint delegatePtr);

// 关闭上下文
context.Close();

4. 运行 .NET 应用程序

hostfxr.InitializeForDotnetCommandLine(
    new[] { "dotnet", "MyApp.dll" },
    parameters,
    out HostFXRHandle context);

int exitCode = context.RunApp();
context.Close();

API 概览

NetHost

方法 说明
NetHost.GetHostFxrPath() 调用 nethost.get_hostfxr_path,返回 hostfxr 库完整路径。

HostFXR

方法 说明
Main / MainStartupinfo / MainBundleStartupinfo 直接启动 hostfxr_main 系列入口。
InitializeForDotnetCommandLine 解析命令行参数并初始化运行时上下文。
InitializeForRuntimeConfig 通过 runtimeconfig.json 初始化运行时上下文。
SetErrorWriter / GetDotnetEnvironmentInfo 设置错误输出器 / 枚举已安装的 SDK 与框架。
GetRuntimePropertyValue / SetRuntimePropertyValue / GetRuntimeProperties 读写运行时属性。
RunApp 执行当前上下文对应的 .NET 应用程序。
Close 关闭 hostfxr 上下文。
Dispose 释放 hostfxr 库句柄。

HostFXRHandle

方法 说明
LoadAssemblyAndGetFunctionPointer 加载程序集并获取类型方法委托。
GetFunctionPointer 获取已加载程序集中类型的方法指针。
LoadAssembly 加载程序集到运行时上下文。
LoadAssemblyBytes 从内存字节加载程序集(可选 PDB)。
GetRuntimePropertyValue / SetRuntimePropertyValue / GetRuntimeProperties 上下文属性读写。
RunApp 运行上下文应用。
Close / Dispose 关闭上下文。

DelegateType

对应 hostfxr_delegate_type,包括:

  • LoadAssemblyAndGetFunctionPointer
  • GetFunctionPointer
  • LoadAssembly
  • LoadAssemblyBytes
  • ComActivation
  • WinrtActivation
  • ComRegister / ComUnregister
  • LoadInMemoryAssembly

NativeAOT 支持

Shimakaze.NetHost 随 NuGet 包附带 NetHost.targets,它会自动:

  1. 解析当前 Runtime Identifier(RuntimeIdentifier / DefaultAppHostRuntimeIdentifier / NETCoreSdkRuntimeIdentifier)。
  2. 从 SDK targeting pack 或 NuGet 全局包目录定位 Microsoft.NETCore.App.Host.<RID>
  3. libnethost.a / libnethost.lib 作为 NativeLibrary 传入 NativeAOT 链接器。
  4. 声明 DirectPInvokenethost,确保 P/Invoke 可被静态解析。
  5. 在 Linux / macOS 上附加 -lstdc++ 链接参数。
  6. 把动态库 nethost.dll / libnethost.so / libnethost.dylib 复制到输出目录,以便非 AOT 运行时使用。

使用 Shimakaze.NetHost 时,无需手动下载或配置 nethost 原生库。


构建与打包

本项目使用 .NET SDK 10 + GitVersion.MsBuild 6.x 进行版本管理,Central Package Management 已启用。

# 还原
dotnet restore --graph --artifacts-path artifacts

# 构建 Release
dotnet build --graph --artifacts-path artifacts --configuration Release --no-restore

# 打包(包含符号包)
dotnet pack --graph --artifacts-path artifacts --configuration Release --no-restore --no-build --include-symbols

CI/CD 配置位于 .github/workflows/build.yaml

  • master 分支的 push / pull_request 时触发构建;
  • v* 标签时发布 GitHub Release 并推送 NuGet 包。

参考项目

相关文档


许可证

本项目采用 MIT License 授权。

Copyright © 2025 frg2089 frg2089@outlook.com

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp1.0 is compatible.  netcoreapp1.1 is compatible.  netcoreapp2.0 is compatible.  netcoreapp2.1 is compatible.  netcoreapp2.2 is compatible.  netcoreapp3.0 is compatible.  netcoreapp3.1 is compatible. 
.NET Standard netstandard1.1 is compatible.  netstandard1.2 is compatible.  netstandard1.3 is compatible.  netstandard1.4 is compatible.  netstandard1.5 is compatible.  netstandard1.6 is compatible.  netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net20 is compatible.  net35 is compatible.  net40 is compatible.  net403 was computed.  net45 is compatible.  net451 is compatible.  net452 is compatible.  net46 is compatible.  net461 is compatible.  net462 is compatible.  net463 was computed.  net47 is compatible.  net471 is compatible.  net472 is compatible.  net48 is compatible.  net481 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Universal Windows Platform uap was computed.  uap10.0 was computed. 
Windows Phone wpa81 was computed. 
Windows Store netcore was computed.  netcore45 was computed.  netcore451 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 1.0

  • .NETCoreApp 1.1

  • .NETCoreApp 2.0

    • No dependencies.
  • .NETCoreApp 2.1

    • No dependencies.
  • .NETCoreApp 2.2

    • No dependencies.
  • .NETCoreApp 3.0

    • No dependencies.
  • .NETCoreApp 3.1

    • No dependencies.
  • .NETFramework 2.0

    • No dependencies.
  • .NETFramework 3.5

    • No dependencies.
  • .NETFramework 4.0

    • No dependencies.
  • .NETFramework 4.5

    • No dependencies.
  • .NETFramework 4.5.1

    • No dependencies.
  • .NETFramework 4.5.2

    • No dependencies.
  • .NETFramework 4.6

    • No dependencies.
  • .NETFramework 4.6.1

    • No dependencies.
  • .NETFramework 4.6.2

    • No dependencies.
  • .NETFramework 4.7

    • No dependencies.
  • .NETFramework 4.7.1

    • No dependencies.
  • .NETFramework 4.7.2

    • No dependencies.
  • .NETFramework 4.8

    • No dependencies.
  • .NETFramework 4.8.1

    • No dependencies.
  • .NETStandard 1.1

  • .NETStandard 1.2

  • .NETStandard 1.3

  • .NETStandard 1.4

  • .NETStandard 1.5

  • .NETStandard 1.6

  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.
  • net10.0

    • No dependencies.
  • net5.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.0.10 170 7/8/2026
0.0.9 115 7/8/2026
0.0.8 113 7/8/2026
0.0.7 110 7/7/2026
0.0.6 240 11/23/2025
0.0.5 450 11/23/2025
0.0.4 522 11/22/2025
0.0.3 553 11/22/2025