The Wavelength
The Wavelength

Reputation: 2914

Embed dotnet core (.NET Core) into native applications on Windows/Linux

Are there possibilities to embed dotnet core (.NET Core) environments into native processes?

My use-case would be an existing game-server where I want to offer the possibility to extend the game using C# on .NET Core on both Windows and Linux.

Bonus question: would there also be an AppDomain-concept available like in "oldschool" .NET?

Upvotes: 6

Views: 3462

Answers (3)

Jens Eckervogt
Jens Eckervogt

Reputation: 1

And I have tested embedded gui or windowing system and you can embed into static executable. First use Alpine or Void-Linux ( qemu guest via shared folder ) See more info https://github.com/dotnet/runtimelab/issues/2202

Sorry I will improve my answer after my work. Okay. I will explain you how do you embed gui or game layer framework like glfw or sdl2/3 but I use musl-gcc and dotnet application ues native aot. I hope you have experience about Alpine or Void-Linux.

// EDIT:

AppDomain -> Environment.CurrentDirectory But it works for Net 8.0 - You can use AppDomain.CurrentDomain.BaseDirectory.

If you want load native library ( built in dotnet library ) You need to pass sbyte*

You should to use DeafMan1983.COnversion

Example:

[UnmanagedCallersOnly(EntryPoint = "SayHello")]
public static void SayHello(sbyte* message)
{
    Console.WriteLine(StringFromSbytePointer(message));
}

And you pass project console with loading and passing library like dlopen, dlsym, dlclose and dlerror

Sorry I use only Linux - If you use Windows thzan you can pass LoadLibraryEx, GetProcAddress, FreeLibrary and more...

You can check example AppWithPlugin for NativeAot from my repository and enjoy your happy coding!

I hope that my repository helps your C# into native application :)

Upvotes: 0

David
David

Reputation: 1296

Yes, it is definitely possible.

I found this documentation with examples: .NET Core Hosting Tutorial

If the link does not work anymore, search the web for "hosting .NET Core".

About AppDomains ... I guess not. If you use CoreClrHost.h you can provide an ID for the default AppDomain, but it seems to be the one and only AppDomain. For isolation you can use seperate processes. Maybe AssemblyLoadContext class can also be a help to you.

Upvotes: 1

TerribleDev
TerribleDev

Reputation: 2245

AppDomain is not coming to dotnet core. Parts of the AppDomain api will come over, but nothing directly related to the functionality you speak of. I don't believe you can embed dotnet core applications inside a native process, however inside of a native process you could boot a dotnet core process.

Upvotes: 0

Related Questions