2

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.

TorbenJ
  • 4,462
  • 11
  • 47
  • 84
  • 2
    Possible duplicate of [No AppDomains in .NET Core! Why?](https://stackoverflow.com/questions/27266907/no-appdomains-in-net-core-why) – omajid Sep 06 '18 at 20:13
  • @omajid: .NET Core 2.1 *has* the equivalent of AppDomains merely called something else. – Joshua Sep 07 '18 at 04:07
  • @Joshua Can you point me to any docs/links where I can read up more about that? – omajid Sep 07 '18 at 05:07
  • @omajid thanks for the link. i read this prior to creation of my question here but what I didn't quite get is if there are no AppDomains anymore then why can I create those using the runtime interface in C++ and why are those even created in the official CoreRun application code? I have to look into the host codes for unix/mac, maybe they do something different. – TorbenJ Sep 07 '18 at 08:21
  • @omajid: Try a Google search for AssemblyLoadContext – Joshua Sep 07 '18 at 13:21
  • https://github.com/dotnet/coreclr/issues No place can be better than this. – Lex Li Sep 07 '18 at 15:18

1 Answers1

0

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"

Quantium
  • 1,779
  • 1
  • 14
  • 14