{"id":20523009,"url":"https://github.com/openbytedev/netcorehost","last_synced_at":"2025-05-16T13:03:44.078Z","repository":{"id":38289014,"uuid":"343816790","full_name":"OpenByteDev/netcorehost","owner":"OpenByteDev","description":"A .NET Core hosting library written in Rust with included bindings for nethost and hostfxr.","archived":false,"fork":false,"pushed_at":"2025-02-09T15:38:55.000Z","size":64230,"stargazers_count":116,"open_issues_count":3,"forks_count":9,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-05-14T18:54:56.602Z","etag":null,"topics":["coreclr","dotnet-core","hostfxr","hosting","nethost"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/OpenByteDev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-03-02T15:15:20.000Z","updated_at":"2025-04-23T14:52:11.000Z","dependencies_parsed_at":"2025-02-19T22:10:21.591Z","dependency_job_id":"7ee154c7-e29a-4d40-931f-1f42c75b40ef","html_url":"https://github.com/OpenByteDev/netcorehost","commit_stats":{"total_commits":375,"total_committers":3,"mean_commits":125.0,"dds":"0.010666666666666713","last_synced_commit":"8e3e5712feef1cd1ad3818e78f4aaa8b8f823e66"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenByteDev%2Fnetcorehost","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenByteDev%2Fnetcorehost/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenByteDev%2Fnetcorehost/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenByteDev%2Fnetcorehost/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/OpenByteDev","download_url":"https://codeload.github.com/OpenByteDev/netcorehost/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254535826,"owners_count":22087398,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["coreclr","dotnet-core","hostfxr","hosting","nethost"],"created_at":"2024-11-15T22:37:34.508Z","updated_at":"2025-05-16T13:03:44.005Z","avatar_url":"https://github.com/OpenByteDev.png","language":"Rust","readme":"# netcorehost\n\n[![CI](https://github.com/OpenByteDev/netcorehost/actions/workflows/ci.yml/badge.svg)](https://github.com/OpenByteDev/netcorehost/actions/workflows/ci.yml) [![crates.io](https://img.shields.io/crates/v/netcorehost.svg)](https://crates.io/crates/netcorehost) [![Documentation](https://docs.rs/netcorehost/badge.svg)](https://docs.rs/netcorehost) [![dependency status](https://deps.rs/repo/github/openbytedev/netcorehost/status.svg)](https://deps.rs/repo/github/openbytedev/netcorehost) [![MIT](https://img.shields.io/crates/l/netcorehost.svg)](https://github.com/OpenByteDev/netcorehost/blob/master/LICENSE)\n\n\u003c!-- cargo-sync-readme start --\u003e\n\nA Rust library for hosting the .NET Core runtime.\n\nIt utilizes the .NET Core hosting API to load and execute managed code from withing the current process.\n\n## Usage\n### Running an application\nThe example below will setup the runtime, load `Test.dll` and run its `Main` method:\n```rust\nlet hostfxr = nethost::load_hostfxr().unwrap();\nlet context = hostfxr.initialize_for_dotnet_command_line(pdcstr!(\"Test.dll\")).unwrap();\nlet result = context.run_app().value();\n```\nThe full example can be found in [examples/run-app](https://github.com/OpenByteDev/netcorehost/tree/master/examples/run-app).\n\n### Calling a managed function\nA function pointer to a managed method can be aquired using an [`AssemblyDelegateLoader`](https://docs.rs/netcorehost/*/netcorehost/hostfxr/struct.AssemblyDelegateLoader.html).\nThis is only supported for [`HostfxrContext`'s](https://docs.rs/netcorehost/*/netcorehost/hostfxr/struct.HostfxrContext.html) that are initialized using [`Hostfxr::initialize_for_runtime_config`](https://docs.rs/netcorehost/*/netcorehost/hostfxr/struct.Hostfxr.html#method.initialize_for_runtime_config). The [`runtimeconfig.json`](https://docs.microsoft.com/en-us/dotnet/core/run-time-config/) is automatically generated for executables, for libraries it is neccessary to add  `\u003cGenerateRuntimeConfigurationFiles\u003eTrue\u003c/GenerateRuntimeConfigurationFiles\u003e` to the projects `.csproj` file.\n\n#### Using the default signature\nThe default method signature is defined as follows:\n```csharp\npublic delegate int ComponentEntryPoint(IntPtr args, int sizeBytes);\n```\n\nA method with the default signature (see code below) can be loaded using [`AssemblyDelegateLoader::get_function_with_default_signature`](https://docs.rs/netcorehost/*/netcorehost/hostfxr/struct.AssemblyDelegateLoader.html#method.get_function_with_default_signature).\n\n**C#**\n```cs\nusing System;\n\nnamespace Test {\n    public static class Program {\n        public static int Hello(IntPtr args, int sizeBytes) {\n            Console.WriteLine(\"Hello from C#!\");\n            return 42;\n        }\n    }\n}\n```\n\n**Rust**\n```rust\nlet hostfxr = nethost::load_hostfxr().unwrap();\nlet context =\n    hostfxr.initialize_for_runtime_config(pdcstr!(\"Test.runtimeconfig.json\")).unwrap();\nlet fn_loader =\n    context.get_delegate_loader_for_assembly(pdcstr!(\"Test.dll\")).unwrap();\nlet hello = fn_loader.get_function_with_default_signature(\n    pdcstr!(\"Test.Program, Test\"),\n    pdcstr!(\"Hello\"),\n).unwrap();\nlet result = unsafe { hello(std::ptr::null(), 0) };\nassert_eq!(result, 42);\n```\n\n#### Using UnmanagedCallersOnly\nA function pointer to a method annotated with [`UnmanagedCallersOnly`](https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.unmanagedcallersonlyattribute) can be loaded without specifying its signature (as these methods cannot be overloaded).\n\n**C#**\n```cs\nusing System;\nusing System.Runtime.InteropServices;\n\nnamespace Test {\n    public static class Program {\n        [UnmanagedCallersOnly]\n        public static void UnmanagedHello() {\n            Console.WriteLine(\"Hello from C#!\");\n        }\n    }\n}\n```\n\n**Rust**\n```rust\nlet hostfxr = nethost::load_hostfxr().unwrap();\nlet context =\n    hostfxr.initialize_for_runtime_config(pdcstr!(\"Test.runtimeconfig.json\")).unwrap();\nlet fn_loader =\n    context.get_delegate_loader_for_assembly(pdcstr!(\"Test.dll\")).unwrap();\nlet hello = fn_loader.get_function_with_unmanaged_callers_only::\u003cfn()\u003e(\n    pdcstr!(\"Test.Program, Test\"),\n    pdcstr!(\"UnmanagedHello\"),\n).unwrap();\nhello(); // prints \"Hello from C#!\"\n```\n\n\n#### Specifying the delegate type\nAnother option is to define a custom delegate type and passing its assembly qualified name to [`AssemblyDelegateLoader::get_function`](https://docs.rs/netcorehost/*/netcorehost/hostfxr/struct.AssemblyDelegateLoader.html#method.get_function).\n\n**C#**\n```cs\nusing System;\n\nnamespace Test {\n    public static class Program {\n        public delegate void CustomHelloFunc();\n    \n        public static void CustomHello() {\n            Console.WriteLine(\"Hello from C#!\");\n        }\n    }\n}\n```\n\n**Rust**\n```rust\nlet hostfxr = nethost::load_hostfxr().unwrap();\nlet context =\n    hostfxr.initialize_for_runtime_config(pdcstr!(\"Test.runtimeconfig.json\")).unwrap();\nlet fn_loader =\n    context.get_delegate_loader_for_assembly(pdcstr!(\"Test.dll\")).unwrap();\nlet hello = fn_loader.get_function::\u003cfn()\u003e(\n    pdcstr!(\"Test.Program, Test\"),\n    pdcstr!(\"CustomHello\"),\n    pdcstr!(\"Test.Program+CustomHelloFunc, Test\")\n).unwrap();\nhello(); // prints \"Hello from C#!\"\n```\n\nThe full examples can be found in [examples/call-managed-function](https://github.com/OpenByteDev/netcorehost/tree/master/examples/call-managed-function).\n\n### Passing complex parameters\nExamples for passing non-primitive parameters can be found in [examples/passing-parameters](https://github.com/OpenByteDev/netcorehost/tree/master/examples/passing-parameters).\n\n## Features\n- `nethost` - Links against nethost and allows for automatic detection of the hostfxr library.\n- `download-nethost` - Automatically downloads the latest nethost binary from [NuGet](https://www.nuget.org/packages/Microsoft.NETCore.DotNetHost/).\n\n\u003c!-- cargo-sync-readme end --\u003e\n\n\n## Related crates\n- [nethost-sys](https://crates.io/crates/nethost-sys) - bindings for the nethost library.\n- [hostfxr-sys](https://crates.io/crates/hostfxr-sys) - bindings for the hostfxr library.\n- [coreclr-hosting-shared](https://crates.io/crates/coreclr-hosting-shared) - shared bindings between [hostfxr-sys](https://crates.io/crates/hostfxr-sys) and [nethost-sys](https://crates.io/crates/nethost-sys).\n\n## Additional Information\n- [Hosting layer APIs](https://github.com/dotnet/core-setup/blob/master/Documentation/design-docs/hosting-layer-apis.md)\n- [Native hosting](https://github.com/dotnet/core-setup/blob/master/Documentation/design-docs/native-hosting.md#runtime-properties)\n- [Write a custom .NET Core host to control the .NET runtime from your native code](https://docs.microsoft.com/en-us/dotnet/core/tutorials/netcore-hosting)\n\n## License\nLicensed under the MIT license ([LICENSE](https://github.com/OpenByteDev/netcorehost/blob/master/LICENSE) or http://opensource.org/licenses/MIT)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenbytedev%2Fnetcorehost","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopenbytedev%2Fnetcorehost","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenbytedev%2Fnetcorehost/lists"}