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.
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.
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.
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.