netcorehost

A Rust library for hosting the .NET Core runtime

31 unstable releases

0.17.0 Feb 2, 2024
0.15.1 Aug 11, 2023
0.15.0 Jul 31, 2023
0.13.1 Nov 29, 2022
0.1.0 Mar 4, 2021
Download history 53/week @ 2024-07-08 85/week @ 2024-07-15 17/week @ 2024-07-22 127/week @ 2024-07-29 31/week @ 2024-08-05 47/week @ 2024-08-12 50/week @ 2024-08-19 75/week @ 2024-08-26 137/week @ 2024-09-02 34/week @ 2024-09-09 68/week @ 2024-09-16 188/week @ 2024-09-23 50/week @ 2024-09-30 160/week @ 2024-10-07 171/week @ 2024-10-14 189/week @ 2024-10-21

578 downloads per month
Used in 4 crates (2 directly)

MIT license

170KB
2.5K SLoC

netcorehost

CI crates.io Documentation dependency status MIT

A Rust library for hosting the .NET Core runtime.

It utilizes the .NET Core hosting API to load and execute managed code from withing the current process.

Usage

Running an application

The example below will setup the runtime, load Test.dll and run its Main method:

let hostfxr = nethost::load_hostfxr().unwrap();
let context = hostfxr.initialize_for_dotnet_command_line(pdcstr!("Test.dll")).unwrap();
let result = context.run_app().value();

The full example can be found in examples/run-app.

Calling a managed function

A function pointer to a managed method can be aquired using an AssemblyDelegateLoader. This is only supported for HostfxrContext's that are initialized using Hostfxr::initialize_for_runtime_config. The runtimeconfig.json is automatically generated for executables, for libraries it is neccessary to add <GenerateRuntimeConfigurationFiles>True</GenerateRuntimeConfigurationFiles> to the projects .csproj file.

Using the default signature

The default method signature is defined as follows:

public delegate int ComponentEntryPoint(IntPtr args, int sizeBytes);

A method with the default signature (see code below) can be loaded using AssemblyDelegateLoader::get_function_with_default_signature.

C#

using System;

namespace Test {
    public static class Program {
        public static int Hello(IntPtr args, int sizeBytes) {
            Console.WriteLine("Hello from C#!");
            return 42;
        }
    }
}

Rust

let hostfxr = nethost::load_hostfxr().unwrap();
let context =
    hostfxr.initialize_for_runtime_config(pdcstr!("Test.runtimeconfig.json")).unwrap();
let fn_loader =
    context.get_delegate_loader_for_assembly(pdcstr!("Test.dll")).unwrap();
let hello = fn_loader.get_function_with_default_signature(
    pdcstr!("Test.Program, Test"),
    pdcstr!("Hello"),
).unwrap();
let result = unsafe { hello(std::ptr::null(), 0) };
assert_eq!(result, 42);

Using UnmanagedCallersOnly

A function pointer to a method annotated with UnmanagedCallersOnly can be loaded without specifying its signature (as these methods cannot be overloaded).

C#

using System;
using System.Runtime.InteropServices;

namespace Test {
    public static class Program {
        [UnmanagedCallersOnly]
        public static void UnmanagedHello() {
            Console.WriteLine("Hello from C#!");
        }
    }
}

Rust

let hostfxr = nethost::load_hostfxr().unwrap();
let context =
    hostfxr.initialize_for_runtime_config(pdcstr!("Test.runtimeconfig.json")).unwrap();
let fn_loader =
    context.get_delegate_loader_for_assembly(pdcstr!("Test.dll")).unwrap();
let hello = fn_loader.get_function_with_unmanaged_callers_only::<fn()>(
    pdcstr!("Test.Program, Test"),
    pdcstr!("UnmanagedHello"),
).unwrap();
hello(); // prints "Hello from C#!"

Specifying the delegate type

Another option is to define a custom delegate type and passing its assembly qualified name to AssemblyDelegateLoader::get_function.

C#

using System;

namespace Test {
    public static class Program {
        public delegate void CustomHelloFunc();
    
        public static void CustomHello() {
            Console.WriteLine("Hello from C#!");
        }
    }
}

Rust

let hostfxr = nethost::load_hostfxr().unwrap();
let context =
    hostfxr.initialize_for_runtime_config(pdcstr!("Test.runtimeconfig.json")).unwrap();
let fn_loader =
    context.get_delegate_loader_for_assembly(pdcstr!("Test.dll")).unwrap();
let hello = fn_loader.get_function::<fn()>(
    pdcstr!("Test.Program, Test"),
    pdcstr!("CustomHello"),
    pdcstr!("Test.Program+CustomHelloFunc, Test")
).unwrap();
hello(); // prints "Hello from C#!"

The full examples can be found in examples/call-managed-function.

Passing complex parameters

Examples for passing non-primitive parameters can be found in examples/passing-parameters.

Features

  • nethost - Links against nethost and allows for automatic detection of the hostfxr library.
  • download-nethost - Automatically downloads the latest nethost binary from NuGet.

Additional Information

License

Licensed under the MIT license (LICENSE or http://opensource.org/licenses/MIT)

Dependencies

~0.6–2.5MB
~35K SLoC