Egor
Egor

Reputation: 175

Load assembly into child AppDomain and releasing dll file

I have child app-domain where I want to load some dll libraries on start-up and release files so that anybody will be able to delete them.

On start-up I do

Loader al = (Loader)domain.CreateInstanceAndUnwrap(
typeof(Loader).Assembly.FullName,
typeof(Loader).FullName);
al.Load(path)

for the following class.

class Loader : MarshalByRefObject
{
    internal void Load(string path)
    {
        Assembly assembly;
        try
        {
            assembly = Assembly.Load(File.ReadAllBytes(path));
        }
        catch (Exception) { return; }
    }
    internal UseType(string fullyQualifiedTypeName)
    {
         Type userType = Type.GetType(fullyQualifiedTypeName);
    }
}

Later I invoke UseType and I get the correct type but I am not able to delete the file any more because it is as if the child app-domain has locked the dll.

Basically what I want to achieve is to cache the assembly file on start-up and later use GetType calls so that the actual dll file will be released.

Is it really possible to achieve something like this ?

Upvotes: 5

Views: 1866

Answers (1)

Travis
Travis

Reputation: 10547

Use shadow copy when you create the App Domain. That copies the dlls into a cache and anyone can interact with the file system.

Topshelf does this with our shelving (everything lives in it's own app domain then) - https://github.com/Topshelf/Topshelf/blob/v2.3/src/Topshelf/Model/ShelfReference.cs#L126.

Update: Topshelf no longer does this, but updated a link to a version which did.

Upvotes: 5

Related Questions