Bug 56821

Summary: Static ctor of MarshalByRefObject runs in primary AppDomain
Product: [Mono] Runtime Reporter: Kirill Osenkov <kirill.osenkov>
Component: GeneralAssignee: Bugzilla <bugzilla>
Status: RESOLVED FIXED    
Severity: normal CC: mono-bugs+mono, mono-bugs+runtime, vargaz
Priority: ---    
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows   
Tags: Is this bug a regression?: ---
Last known good build:

Description Kirill Osenkov 2017-05-25 00:14:02 UTC
I'm using Mono 5.2.0.127 on Windows.

Consider this C# program:
=========================
using System;

class Program
{
    static void Main(string[] args)
    {
        var appDomain = AppDomain.CreateDomain("Test Domain", null, AppDomain.CurrentDomain.SetupInformation);
        var driver = (Remote)appDomain.CreateInstanceAndUnwrap(
            typeof(Remote).Assembly.FullName,
            typeof(Remote).FullName);
        driver.Test();
    }
}

class Remote : MarshalByRefObject
{
    static Remote()
    {
        Console.WriteLine($"cctor in AppDomain {AppDomain.CurrentDomain.FriendlyName}");
    }

    internal void Test()
    {
    }
}

=========================

If you compile and run this C# file on .NET Framework, the output is:

cctor in AppDomain Test Domain

If you run it on Mono, the output is:

cctor in AppDomain Test Domain
cctor in AppDomain CrashMono.exe

Not sure if this behavior is by design, but I thought I'd check just in case. Why is the cctor not running in the primary appdomain for .NET Framework but is running on Mono?

I'm noticing another discrepancy, not sure if related:

Assembly.Load("System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"); loads three assemblies on .NET (System.Windows.Forms, System and System.Drawing) but only one assembly on Mono (System.Windows.Forms)
Comment 1 Zoltan Varga 2017-05-30 20:58:51 UTC
https://github.com/mono/mono/pull/4950
Comment 2 Zoltan Varga 2017-06-02 15:58:07 UTC
Fixed in master/2017-04.