GithubHelp home page GithubHelp logo

ryu-s / netcorehost Goto Github PK

View Code? Open in Web Editor NEW

This project forked from openbytedev/netcorehost

0.0 1.0 0.0 80.54 MB

A .NET Core hosting library written in Rust with included bindings for nethost and hostfxr.

License: MIT License

Rust 99.57% C# 0.43%

netcorehost's Introduction

netcorehost

Build crates.io Documentation dependency status MIT

A Rust library for hosting .NET Core applications.

This crate allows a .NET Core app to be run inside the current process or to load it and call a contained method directly.

Basic Usage

The first step is to load the hostfxr library with handles the hosting. This can be done with nethost::load_hostfxr() which automatically locates the library on your system. The returned instance of Hostfxr can then be used to initialize a new HostfxrContext through one of the initialize_* methods like initialize_for_dotnet_command_line which in turn be used to run the app associated with the context or to load a pointer to a function od the loaded library.

Examples

Running an application

Assuming the following app located inside the current folder and named Test.dll:

public static class Program {
    public static int Main() {
        System.Console.WriteLine("Hello from C#!");
    }
}

The following code will setup the hostfxr library, load the app and run its Main method.

let assembly_path = PdCString::from_str("./Test.dll")?;

let hostfxr = nethost::load_hostfxr()?;
let context = hostfxr.initialize_for_dotnet_command_line(assembly_path)?;
let result = context.run_app();

Calling a managed function

Assuming the following app located inside the current folder and named Test.dll:

namespace Test {
    public static class Program {
        public static int Hello(IntPtr arg, int argLength) {
            return 42;
        }
    }
}

The following code will setup the hostfxr library, load the app and call the Hello method. The method has the default signature which avoids having to specify it. It accepts a ptr to some data and the size of said data.

let hostfxr = nethost::load_hostfxr()?;
let context =
    hostfxr.initialize_for_runtime_config(PdCString::from_str("Test.runtimeconfig.json")?)?;
let fn_loader =
    context.get_delegate_loader_for_assembly(PdCString::from_str("Test.dll")?)?;
let hello = fn_loader.get_function_pointer_with_default_signature(
    PdCString::from_str("Test.Program, Test")?,
    PdCString::from_str("Hello")?,
);
let result = unsafe { hello(ptr::null(), 0) };

Alternatively it is possible to call a method with any signature if it is annotated with UnmanagedCallersOnly (loaded with get_function_pointer_for_unmanaged_callers_only_method) or if the signature is passed to get_function_pointer.

Additional Information

License

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

netcorehost's People

Contributors

openbytedev avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.