mercredi 7 octobre 2015

Storing instance of object created under AppDomain

Iam trying to use AppDomains in order to isolate and run code in my application. What I need to do, is to create new AppDomain for each client and under that AppDomain, I need to create instance of another class which contains the code that needs to be isolated. I also need to somehow store that created instance, so that I can access it later when the same client that created it calls again. The way Im creating it now is this:

private Dictionary<string, IsolatedClass> isolatedClassesList = new Dictionary<string, IsolatedClass>();

public void Initialize(string clientId)
{
    AppDomain appDomain = AppDomain.CreateDomain("New AppDomain");

    IsolatedClass isolatedClass = (IsolatedClass)appDomain.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, typeof(IsolatedClass).FullName);

    isolatedClass.Initialize(clientId);

    isolatedClassesList.Add(clientId, isolatedClass);
}

The creation of AppDomain and first call of "Initiliaze(clientId)" method works fine. Also storing it in the dictionary (for later use) is done without any exceptions.

The problem occurs when I try to get the instance of previously created IsolatedClass later, like that:

public void DoSomething (string clientId)
{
    IsolatedClass isolatedClass = isolatedClassesList.First(x => x.Key == clientId).Value;

    isolatedClass.RunIsolatedMethod();
}

It throws null refference exception (it cannot retrieve the instance). When I put a breakpoint there and check what is inside the dictionary, for Value, it shows me that: "Obtaining the runtime type of a transparent proxy is not supported in this context."

Is this totally wrong approach or is there just some minor mistake? If my approach is totally wrong, is there any other way how to achieve my goal?





Aucun commentaire:

Enregistrer un commentaire