locked
application domain, and verification of assumptions RRS feed

  • Question

  • Hi All,

    I am learning about appDomains and am hoping someone can confirm that my observation's are correct.  My goal on using appdomain's is to make certain that a class and it's heierarchy of methods and threads are isolated.  I do understand the relationship between processes and app domains.

    I am including the following code as a point of reference to my questions:

    //
    // The following class and method reside in it's own dll/lib file
    //  
    
     [SerializableAttribute]
        public class Class1 : MarshalByRefObject
        {
            public Class1()
            {
            }
    
            public void Method1(int x)
            {
                Console.WriteLine("Current Domain {0} ID = {1}", AppDomain.CurrentDomain.FriendlyName, AppDomain.CurrentDomain.Id);
                Console.WriteLine("Method1 int = {0}", x);
            }   
        }
    
    //
    // the following interface resides in its own dll/lib file
    //
      public interface Interface1
        {
            void Method1();
        }
    
    //
    // the following resides in a exe
    //
        class Program
        {
            static void Main(string[] args)
            {
                myHost a = new myHost();
                myHost b = new myHost();
    
                a.Method1();
             
                b.Method1();
    
                Console.ReadLine();
            }
        }
    
        public class myHost : Interface1
        {
            private AppDomain fredsDomain;
            private System.Reflection.Assembly hanksAssembly;
            private Class1 xClass1;
            
            public myHost()
            {
                fredsDomain = AppDomain.CreateDomain("freds domain");
                xClass1 = (Class1) fredsDomain.CreateInstanceFromAndUnwrap("AppTest.dll", "ConsoleApplication4.Class1");
            }
    
            public void Method1()
            {
                Console.WriteLine("Current Domain {0}", AppDomain.CurrentDomain.FriendlyName);
                this.xClass1.Method1(100);
            }
        }

    So Here are my questions:

    1. Is this correct App domains can have the same name and what makes them unique is there ID?
    2. Classes in a AppDomain (at least those that are being called across domain boundries need to have the following characteristics:
    a. Classes need to have a serializable attribute.
    b. Method calls are marshalled by ref

    3. Do methods that return values across app domain boundries need to be marshalled?
    4. In the above example does Class1 need to be in it's own DLL or can it exsist in the same dll/lib as Program class?

    Thanks,


    Hank



    hank voight
    Friday, August 14, 2009 11:37 PM

Answers

  • Hi,

    Not sure on #1.

    As for #2, I believe both a & b are correct.

    3. I believe the methods need to be on a class that is inherits from MarshalByRefObject and the return type must be serialisable. The return type must also exist in the calling app domain, if not you'll either get an error or the type will automatically be loaded into the calling domain for you which will also cause the assembly containing it to the be loaded in the calling app domain, which can then cause you other problems and prevent you from unloading the assembly entirely if that's what you want to do.

    4. I believe for this to be useful class1 needs to be in it's own dll. I think it could be in the same one as the rest of the program, but then I think you're going to load two copies of the assembly, one into each app domain, and each app domain will have code that is unnecessary for it. Also, you'll have a problem once again if you ever want to unload the assembly fully.
    • Marked as answer by hank voight Sunday, August 16, 2009 7:47 PM
    Saturday, August 15, 2009 10:20 PM
  • Hi Hank,

     

    As you have marked Yort’s reply as the answer, I think Yort is correct for the question 2, 3, and 4.   Thank you, Yort!

     

    As for the first question, Hank, you are right!   We can have two AppDomain with same friendly name in one .NET process.  We have a AppDomain id to uniquely identifies the application domain within the process. 

     

    http://msdn.microsoft.com/en-us/library/system.appdomain.id.aspx
    http://msdn.microsoft.com/en-us/library/system.appdomain.friendlyname.aspx

     

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, August 17, 2009 9:29 AM
    Moderator

All replies

  • Hi,

    Not sure on #1.

    As for #2, I believe both a & b are correct.

    3. I believe the methods need to be on a class that is inherits from MarshalByRefObject and the return type must be serialisable. The return type must also exist in the calling app domain, if not you'll either get an error or the type will automatically be loaded into the calling domain for you which will also cause the assembly containing it to the be loaded in the calling app domain, which can then cause you other problems and prevent you from unloading the assembly entirely if that's what you want to do.

    4. I believe for this to be useful class1 needs to be in it's own dll. I think it could be in the same one as the rest of the program, but then I think you're going to load two copies of the assembly, one into each app domain, and each app domain will have code that is unnecessary for it. Also, you'll have a problem once again if you ever want to unload the assembly fully.
    • Marked as answer by hank voight Sunday, August 16, 2009 7:47 PM
    Saturday, August 15, 2009 10:20 PM
  • Hi Hank,

     

    As you have marked Yort’s reply as the answer, I think Yort is correct for the question 2, 3, and 4.   Thank you, Yort!

     

    As for the first question, Hank, you are right!   We can have two AppDomain with same friendly name in one .NET process.  We have a AppDomain id to uniquely identifies the application domain within the process. 

     

    http://msdn.microsoft.com/en-us/library/system.appdomain.id.aspx
    http://msdn.microsoft.com/en-us/library/system.appdomain.friendlyname.aspx

     

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, August 17, 2009 9:29 AM
    Moderator