none
How to load an assembly in new appdomain RRS feed

  • Question

  • Hi All,

    I am loading an assembly in another appdomain but facing an issue. This is the code I have created a class that i am loading into a  new domain. and then I load an assembly using LoadAssembly(Path) method in the new loader class. now the issue is that when I check loaded assemblies in the current domain i.e. main domainI can see all the assemblies that I have loaded in new domain and when I check newdomain.assemblyloaded i can see only 2 - 3 assemblies that were loaded at the time of initializing the object. How can i load the assemblies in newdomain only and not in main appdomain. This is the code.

    Loader class is like this:

    [Serializable]

        public class Loader

        {

            private Assembly _assembly;

            System.Type MyType = null;

            MethodInfo MyMethod = null;

            string version = string.Empty;

     

            public Version LoadAssembly(string path)

            {

                _assembly = Assembly.Load(AssemblyName.GetAssemblyName(path));

               

                foreach (System.Type type in _assembly.GetTypes())

                {

                    if (String.Compare(type.Name, "MyClass", true) == 0)

                    {

                        MyType = type;

                        break;

                    }

                }

                return _assembly.GetName().Version;

            }

     

            public object ExecuteMethod(string MethodName, params object[] parameters)

            {

                        MyMethod = MyType.GetMethod(MethodName, new Type[] { typeof(string), typeof(string), typeof(string), typeof(string) });

                        break;

                   

    }

    Main class is:

    setup = AppDomain.CurrentDomain.SetupInformation;

                NewDomain = AppDomain.CreateDomain("newDomain", AppDomain.CurrentDomain.Evidence, setup);

                loader = (Loader)NewDomain.CreateInstanceAndUnwrap(typeof(Loader).Assembly.FullName, typeof(Loader).FullName);

    loader.LoadAssembly(Path.Combine(InstallationPath, @"MyAssembly.dll"));

     

     ////////////////////////////////////////////////////////////////////

     AppDomain MyDomain = AppDomain.CurrentDomain;
     Assembly[] AssembliesLoaded = MyDomain.GetAssemblies();

    //The above two lines of code gives me the dlls that I have loaded in the NewDomain.
     ////////////////////////////////////////////////////////////////////

    loader.ExecuteMethod("Start", new object[] { m_EnvHashTable })

     

    Please suggest me something so that the assemblies will only be loaded into the NewDomain and not in current main domain.

     

    Thanks & Regards,
    Girish Nehte

     

    • Moved by Helen Zhou Wednesday, September 28, 2011 2:52 AM (From:Windows Forms General)
    Monday, September 26, 2011 2:59 PM

Answers

  • Forums participant Louis.fr once posted a quick and dirty code snippet that creates an AppDomain, loads an assembly, and then makes a method call in the second AppDomain. It's better than what I have. I once posted it at this link.

    C# Load DLL in separate domain and call a method

    The basic problem that you are running into is when you call "Load", then the assembly is loaded into the appdomain that issued the call.  Notice how Louis.fr's sample carefully discriminates between which domain calls which method.

    Rudy =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/


    that link seems to be broken.  Here's the code snippet.

     

    public class Loader : MarshalByRefObject
     {
         object CallInternal(string dll, string typename, string method, object[] parameters)
         {
             Assembly a = Assembly.LoadFile(dll);
             object o = a.CreateInstance(typename);
             Type t = o.GetType();
             MethodInfo m = t.GetMethod(method);
             return m.Invoke(o, parameters);
         }
         public static object Call(string dll, string typename, string method, params object[] parameters)
         {
             AppDomain dom = AppDomain.CreateDomain("Volatile");
             Loader ld = (Loader)dom.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, typeof(Loader).FullName);
             object result = ld.CallInternal(dll, typename, method, parameters);
             AppDomain.Unload(dom);
             return result;
         }
     }
    


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    • Marked as answer by Paul Zhou Tuesday, October 4, 2011 5:54 AM
    Tuesday, September 27, 2011 12:07 AM

All replies

  • Monday, September 26, 2011 4:39 PM
  • Forums participant Louis.fr once posted a quick and dirty code snippet that creates an AppDomain, loads an assembly, and then makes a method call in the second AppDomain. It's better than what I have. I once posted it at this link.

    C# Load DLL in separate domain and call a method

    The basic problem that you are running into is when you call "Load", then the assembly is loaded into the appdomain that issued the call.  Notice how Louis.fr's sample carefully discriminates between which domain calls which method.

    Rudy =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Monday, September 26, 2011 11:42 PM
  • Forums participant Louis.fr once posted a quick and dirty code snippet that creates an AppDomain, loads an assembly, and then makes a method call in the second AppDomain. It's better than what I have. I once posted it at this link.

    C# Load DLL in separate domain and call a method

    The basic problem that you are running into is when you call "Load", then the assembly is loaded into the appdomain that issued the call.  Notice how Louis.fr's sample carefully discriminates between which domain calls which method.

    Rudy =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/


    that link seems to be broken.  Here's the code snippet.

     

    public class Loader : MarshalByRefObject
     {
         object CallInternal(string dll, string typename, string method, object[] parameters)
         {
             Assembly a = Assembly.LoadFile(dll);
             object o = a.CreateInstance(typename);
             Type t = o.GetType();
             MethodInfo m = t.GetMethod(method);
             return m.Invoke(o, parameters);
         }
         public static object Call(string dll, string typename, string method, params object[] parameters)
         {
             AppDomain dom = AppDomain.CreateDomain("Volatile");
             Loader ld = (Loader)dom.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, typeof(Loader).FullName);
             object result = ld.CallInternal(dll, typename, method, parameters);
             AppDomain.Unload(dom);
             return result;
         }
     }
    


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    • Marked as answer by Paul Zhou Tuesday, October 4, 2011 5:54 AM
    Tuesday, September 27, 2011 12:07 AM
  • Rudy,

    I seldom have problems understanding text written in the English language.

    Can you tell me what I read all the time wrong in this sentence.

    This is the description of this forum.

    Discuss client application development using Windows Forms controls and features

    In my idea is your reply in no way related to that?

    If you had done it in VB I maybe would have understand it.

    :-) Just joking.

    But somehow I get the idea that because of that some moderators move to much questions from the C# forum to this forum, this form becomes (because of how the search engine works of MSDN) a kind of dependence from the C# forum.


    Success
    Cor

    • Edited by Cor Ligthert Tuesday, September 27, 2011 6:26 AM
    Tuesday, September 27, 2011 6:22 AM
  • Hello Girish,

    This is off topic in WinForms forum which is for UI control issues. It will be more appropriate to be moved to Common Language Runtime forum to get better help. Thank you for your understanding.

    But please check if those same topic threads would give some of help:
    Load Assembly into new AppDomain
    Load assembly in the new AppDomain

    Hope them help.

    Best regards,


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by Helen Zhou Wednesday, September 28, 2011 2:52 AM
    Wednesday, September 28, 2011 2:46 AM