0
votes

Is appdomain unloads at time when I call AppDomain.Unload(name) method. Or it flags for the next garbage collection. I have following situation Collection of AppDomains. ListApp Unload the particular domain say d1 from ListApp Immediately after unloading d1 again create domain d1 and add it in ListApp When I use d1.CreateInstanceAndUnwrap() method it throws exception "attempted to access an unloaded appdomain"

public static IDictionary<object, AppDomain> LoadedDomains { get; internal set; }

private static void Load(object key)
{
    if (!LoadedDomains.Contains(key))
    {
        AppDomain domain = AppDomain.CreateDomain("t");
        LoadedDomains.Add(key, domain);
    }
}

private static void UnLoad(object key)
{
    if (LoadedDomains.Contains(key))
    {
        AppDomain.Unload(LoadedDomains(key));
        LoadedDomains.Remove(key);
    }
}

private static void Execute()
{
    object key = new object();
    Load(key);
    Unload(key);
    Load(key);
    MyObject myobj= LoadedDomains[key].CreateInstance("asName", "type");
}
2
It unloads when you call .Unload, but we would have to see your code to figure out why the exception is thrown.BrandonAGr
Unrelated to your AppDomain problem, but your dictionary is declared as having string keys, but you use object as the keys in your methods.Mike Two
After fixing your code so that it compiles, it seems to work for me.svick
@Mike Two Sorry its my fault.meetjaydeep

2 Answers

1
votes

It sounds like you're describing a multi-threading problem, not a garbage collection problem - as in your code is trying to access the domain after it's been unloaded, but before the new one has been created and been assigned to the variable.

Unload synchronously unloads the AppDomain in question and will throw an exception if the domain cannot be unloaded.

Need more code to assist further...

0
votes

I found the problem its in my code. Unloading the domain and not refreshing the dictionary.