0

I am trying to create instance of MyClass by factory method in othe application domain is there any solution to this problem? Edited: The question is how to do it

Thank you.

Sergey Kucher
  • 4,140
  • 5
  • 29
  • 47
  • Tried to create other domain and use domain.Load(assemblypath) but this doesn't work throws FileLoadException – Sergey Kucher Jun 09 '11 at 11:45
  • And what does it say exactly in that exception? – Daniel Hilgarth Jun 09 '11 at 11:48
  • {"Could not load file or assembly 'C:\\location\\mydll.dll' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)":"C:\\location\\mydll.dll" – Sergey Kucher Jun 09 '11 at 11:53
  • but when I use Assembly.LoadFrom("C:\\location\\mydll.dll") it succeds – Sergey Kucher Jun 09 '11 at 11:54
  • Because [`Assembly.Load`](http://msdn.microsoft.com/en-us/library/ky3942xh.aspx) doesn't want a file name but an assembly name, like `SampleAssembly, Version=1.0.2004.0, Culture=neutral, PublicKeyToken=8744b20f8da049e3` – Daniel Hilgarth Jun 09 '11 at 11:57

1 Answers1

2

There are a few options for solving this probelm. Depending on how you want your solution to scale and how complex it already is, you may want to look at MAF (see the System.AddIn namespace) as this handles loading AddIns and supports AppDomain separation already. It also implements a whole load of functionality to do with lifetime management of objects created in the AppDomains and loading/unloading AddIns and versioning.

If you would rather implement your own, or just want more understanding of AppDomains then here is an example that will hopefully help. It doesn't do anything with AppDomain setup, security or lifetime management and to make the code more compact there is no error handling but it can be used as a guide.

If you start with an example where a Person object is created with a factory:

public class Person
{
    internal Person(string name)
    {
        Name = name;
    }

    public string Name { get; private set; }
}

public class PersonFactory
{
    public static Person CreatePerson(string name)
    {
        return new Person(name);
    }
}

class Program
{
    static void Main(string[] args)
    {
        Person p = PersonFactory.CreatePerson("John Smith");
    }
}

You can then add to this to create a Person in another AppDomain with a few fairly simple changes to the code above.

  • Make the class Person a MarshalByRefObject
  • Add another property to Person that will output their AppDomain for testing
  • Add another static method to the factory for creating in a different AppDomain
  • The amended code is:

    public class Person : MarshalByRefObject
    {
        internal Person(string name)
        {
            Name = name;
        }
    
        public string Name { get; private set; }
        public string AppDomainName { get { return AppDomain.CurrentDomain.FriendlyName; } }
    }
    
    public class PersonFactory
    {
        public static Person CreatePerson(string name)
        {
            return new Person(name);
        }
    
        public static Person CreatePersonInAppDomain(string name, AppDomain domain)
        {
            return (Person)domain.CreateInstanceAndUnwrap(
                typeof(Person).Assembly.FullName, 
                typeof(Person).FullName, 
                false, 
                BindingFlags.NonPublic | BindingFlags.Instance, 
                null, 
                new object[] { name }, 
                null, 
                null
                );
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {            
            AppDomain domain = AppDomain.CreateDomain("NewDomain");
    
            Person person1 = PersonFactory.CreatePerson("John Smith");
            Person person2 = PersonFactory.CreatePersonInAppDomain("Jane Smith", domain);
    
            Console.WriteLine("Person: Name={0}, Domain={1}", person1.Name, person1.AppDomainName);
            Console.WriteLine("Person: Name={0}, Domain={1}", person2.Name, person2.AppDomainName);
        }
    }
    

    The output should be:

    Person: Name=John Smith, AppDomain=[your exe name]
    Person: Name=Jane Smith, AppDomain=NewDomain
    

    So, what is going on?

    Since person2 is an object in another AppDomain, it either needs to be Serializable or derive from MarshalByRefObject. In this example, I have derived from MarshalByRefObject so the real instance exists only in the second AppDomain and the reference in the original AppDomain is actually a proxy. If I had chosen the Serializable implementation then a copy of the person would be passed back to the original AppDomain.

    This is something else that needs to be considered as each time the new AppDomain is called any parameters need to be marshalled, which will have some impact on performance. Also, the lifetime of the object needs to be considered too as MarshalByRefObjects will eventually timeout and be collected by the garbage collector. You will need to investigate lifetime management to extend this further.

    Simon
    • 209
    • 1
    • 3