I'm currently experimenting with hosting .NET CoreCLR in a C++ application to basically implement some kind of plugin system.
I used the corerun source code as a base to get started and to explore how to build a native host other tutorials/documentation generally failed in one way or another.
Finally I got it to work, I could initialize the runtime, create an AppDomain and execute a .NET Core assembly.
The next step for me was to run multiple assemblies, so I just created a second test assembly and tried to execute it the same way as the first one.
In fact my application does the following:
Initialize Runtime -> Create AppDomain1 -> Execute Assembly 1 -> Unload AppDomain 1
-> Create AppDomain2 -> Execute Assembly 2 -> Unload AppDomain2
Unfortunately I wasn't able to even create the second AppDomain.
My code for AppDomain creation looks roughly like this:
m_RuntimeHost->CreateAppDomainWithManager
(
L"MyFriendlyName",
appDomainFlags,
nullptr,
nullptr,
sizeof(propertyKeys) / sizeof(wchar_t*),
propertyKeys,
propertyValues,
&domainId
);
My appDomainFlags
are the ones from the corerun
code:
APPDOMAIN_ENABLE_PLATFORM_SPECIFIC_APPS |
APPDOMAIN_ENABLE_PINVOKE_AND_CLASSIC_COMINTEROP |
APPDOMAIN_DISABLE_TRANSPARENCY_ENFORCEMENT
I just assume that all properties I pass here are correct as creation of the AppDomain works at least once.
If I try to create the second AppDomain I just get HRESULT 0x80131022 (Invalid operation)
.
I then went on an examined creation of the runtime interface and replaced the original startup flags of the runtime from:
STARTUP_FLAGS::STARTUP_LOADER_OPTIMIZATION_SINGLE_DOMAIN | STARTUP_FLAGS::STARTUP_SINGLE_APPDOMAIN | STARTUP_FLAGS::STARTUP_CONCURRENT_GC
to:
STARTUP_SERVER_GC | STARTUP_LOADER_OPTIMIZATION_MULTI_DOMAIN | STARTUP_LOADER_OPTIMIZATION_MULTI_DOMAIN_HOST
This was just a guessing game as I don't yet fully understand what exactly these flags do but it didn't change anything unfortunately.
I don't really know any further but I guess I'm missing something important here.
Documentation and articles about this topic are quite rare so I hope that someone here can help me out or give some hints about what might be wrong.
I recently did some research regarding hosting and running multiple AppDomains. Here's what I found in the Microsoft Docs and GitHub site
Step 2 - Get .NET Core hosting functions CoreClrHost has several important methods useful for hosting .NET Core:
coreclr_initialize
: Starts the .NET Core runtime and sets up the default (and only) AppDomain.coreclr_execute_assembly
: Executes a managed assembly.coreclr_create_delegate
: Creates a function pointer to a managed method.coreclr_shutdown
: Shuts down the .NET Core runtime.coreclr_shutdown_2
: Like coreclr_shutdown, but also retrieves the managed code's exit code.
I took this to mean, "you only get to start up one AppDomain"
User contributions licensed under CC BY-SA 3.0