How openbytedev/netcorehost works
Project Type & Purpose
• This is an open-source Rust library (“crate”) that provides a high-level host for the .NET Core runtime.
• It exposes a Rust API to locate, load, and drive the .NET Core runtime (hostfxr/nethost), and call into managed assemblies.
Major Components
a. Consumer (your native host application)
b. netcorehost Rust crate
• High-level API (lib.rs)
• Modules:
– nethost (src/nethost.rs)
– hostfxr (src/hostfxr/*.rs)
– pdcstring (src/pdcstring/*.rs)
– error handling (src/error/*.rs)
• Sys-bindings (src/bindings/mod.rs)
c. Native hosting libraries (externals)
• nethost (nethost library from .NET Core installation or downloaded via feature)
• hostfxr (hostfxr library versions 1.0,2.1,3.0,6.0)
d. .NET Core Runtime
e. Managed code (C# assemblies)
• Test and example projects under /examples and /tests
f. Optional external service
• NuGet (for automatic download-nethost feature)
Relationships & Data Flows
1. Consumer → call into netcorehost’s public API (e.g., load_hostfxr())
2. netcorehost/nethost module → FFI into nethost library to locate hostfxr
3. netcorehost/hostfxr module → dynamically load hostfxr library and invoke hostfxr_initialize_for_*
4. hostfxr → (.NET Core runtime loader) → set up runtime context
5. netcorehost/hostfxr context → run apps or get delegate loader
6. Delegate loader → load managed assembly → resolve function pointer
7. Consumer → invoke managed function → execution in .NET runtime → result back to Rust
Key Architectural Patterns & Principles
• Facade Pattern: netcorehost exposes a simplified Rust API over complex native hosting APIs
• Layered Abstraction:
• High-level Rust modules
• Sys-bindings (unsafe FFI)
• Native hosting libraries (nethost/hostfxr)
• .NET runtime and managed assemblies
• Separation of Concerns: string management (pdcstring), error handling, hosting logic, delegate loading
Minimum Elements
• Consumer application
• netcorehost public API (lib.rs)
• Modules: nethost, hostfxr, pdcstring, error, bindings
• Native libraries: nethost, hostfxr (multiple versions)
• .NET Core runtime
• Managed assemblies (ExampleProject/Test)
• Notes on optional features (download-nethost, runtimeconfig.json generation)
[Consumer App]
↓ calls
[netcorehost API (lib.rs)]
→ uses → [pdcstring]
→ uses → [error module]
→ uses → [bindings (src/bindings)]
↓ FFI calls
[nethost library] ─ locates ─> [hostfxr library] ─ boots ─> [.NET Core runtime]
│ ↓ executes
└──────────── delegates loader ────────> [Managed Assembly (ExampleProject.dll)]
↑ callback
function pointer