Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6719739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:07:43+00:00 2026-05-26T09:07:43+00:00

I have a marshalable class that contains a factory method. The factory method can

  • 0

I have a marshalable class that contains a factory method. The factory method can be used to instantiate the class in a test AppDomain. I’m trying to understand whether I can use the class with the using( … ) dispose pattern.

The principle concern for me is whether the test AppDomain is Unloaded — and when. I’ve added a static flag to the class that’s set when an instance method is invoked. If the appdomain is not unloaded, then I believe this flag should retain it’s setting in subsequent invocations.

A sample class and test console app would look like this:

using System;
using System.Reflection;
using System.Threading;

namespace AppDomainInDispose
{
    public class TestClass : MarshalByRefObject, IDisposable
    {
        public void Run()
        {
            Console.WriteLine("Hello from {0}", Thread.GetDomain().FriendlyName);
            if (_flag)
                Console.WriteLine("Flagged!");
            else
                _flag = true;
        }

        private static bool _flag = false;

        public static TestClass InstantiateInTestDomain()
        {
            var callingDomain = Thread.GetDomain();
            var setup = new AppDomainSetup() { ApplicationBase = callingDomain.SetupInformation.ApplicationBase };
            _domain = AppDomain.CreateDomain("test-domain", null, setup);
            _domain.DomainUnload += _domain_DomainUnload;

            var assembly = Assembly.GetAssembly(typeof(TestClass)).CodeBase;
            var proxy = _domain.CreateInstanceFromAndUnwrap(assembly, "AppDomainInDispose.TestClass") as TestClass;

            return proxy;
        }

        static void _domain_DomainUnload(object sender, EventArgs e)
        {
            Console.WriteLine("Unloading");
        }

        public static AppDomain _domain = null;

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        ~TestClass()
        {
            Dispose(false);
        }

        private void Dispose(bool disposing)
        {
            if(disposing)
            {
                //AppDomain.Unload(_domain);    // can't, as I'm in the AppDomain
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            using(var testClass = TestClass.InstantiateInTestDomain())
            {
                testClass.Run();
            }
            using (var testClass = TestClass.InstantiateInTestDomain())
            {
                testClass.Run();    // if the appdomain hasn't been unloaded then the static flag will still be set
            }

            Console.ReadKey();
        }
    }
}

It seems that in this limited test the AppDomain is being unloaded — but I’m not sure where. Could someone explain what’s going on with the AppDomain? Is this approach a very bad idea?

EDIT: Looks like you can have multiple AppDomains sharing the same name, which I didn’t realise. Also, the DomainUnload event isn’t fired, so I might assume that the domain is not being unloaded. That, or the event is not fired in some circumstances (perhaps while the hosting process is shutting down).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:07:43+00:00Added an answer on May 26, 2026 at 9:07 am

    Hmmm.. it is getting a bit complicated. If you want to be sure that the app domain does get unloaded after the using block end, below is one such implementation. I have separated the 2 concerns here “create app domain and handling of app domain” and “the Type itself that is being loaded in app domain”. (All this is in main prog class)

    public class GenericDisposable<T> : IDisposable
    {
        public Action Dispose { get; set; }
        public T Object { get; set; }
        void IDisposable.Dispose()
        {
            Dispose();
        }
    
    }
    public static GenericDisposable<T> CreateDomainWithType<T>()
    {
        var appDomain = AppDomain.CreateDomain("test-domain");
        var inst = appDomain.CreateInstanceAndUnwrap(typeof(T).Assembly.FullName, typeof(T).FullName);
        appDomain.DomainUnload += (a, b) => Console.WriteLine("Unloaded");
        return new GenericDisposable<T>() { Dispose = () => AppDomain.Unload(appDomain), Object = (T)inst };
    }
    public class User : MarshalByRefObject
    {
        public void Sayhello()
        {
            Console.WriteLine("Hello from User");
        }
    }
    
    //Usage              
    static void Main()
    {
        using (var wrap = CreateDomainWithType<User>())
        {
            wrap.Object.Sayhello();
        }
        Console.Read();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have an app that can use tts to read text messages. It can also
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a
Have created simple Ajax enabled contact forms before that have around 12 fields -
Have a LinqtoSql query that I now want to precompile. var unorderedc = from
Have been trying the whole day long and googled the **** out of the
Have some data in a sybase image type column that I want to use
Have you used NPanday to integrate Visual Studio with Apache Maven ? If so,
have anyone can tell me what syntax error on this actionscript (actionscript3.0)? var rotY:
Have a bunch of WCF REST services hosted on Azure that access a SQL
Have some dates in my local Oracle 11g database that are in this format:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.