meetjaydeep
meetjaydeep

Reputation: 1850

AppDomain.Unload issue

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");
}

Upvotes: 0

Views: 1138

Answers (2)

meetjaydeep
meetjaydeep

Reputation: 1850

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

Upvotes: 0

Jeff
Jeff

Reputation: 36573

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

Upvotes: 1

Related Questions