Results 1 to 5 of 5

Thread: Oops!! How to unload the Assembly *Hell* !!! Nothing works !!

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2000
    Location
    India
    Posts
    22

    Oops!! How to unload the Assembly *Hell* !!! Nothing works !!

    Dear all,

    I opened an Assembly using Assembly.LoadFrom() method, did some work and set null. After that when i tried to delete that assembly file, its locked and couldn't remove it.

    I tried using AppDomain also, to load that assembly in seperate appdomain and unloaded it using AppDomain.UnLoad also. It doesn't work. Nothing works. I can delete only when my app exits. I absolutely need of this assembly unload in my app.

    Experts !!!, how to unload the Assembly *hell*.

    Any help would be greatly appreciated.

    Regards

    Rafik.

  2. #2
    Lively Member
    Join Date
    Aug 1999
    Location
    Amsterdam
    Posts
    117
    You can't unload a specific assembly, only an AppDomain.
    You should use the static Unload method of the AppDomain class.

    AppDomain.Unload(YourAppDomain)


    Note 1
    You can't unload the default AppDomain.

    Note 2
    If you use LoaderOptimization MultiDomain your assemblies get loaded in a domain neutral fashion, then the assembly gets locked until the whole process exits.

  3. #3
    Lively Member
    Join Date
    Aug 1999
    Location
    Amsterdam
    Posts
    117
    Another note, setting a variable to null/nothing worked in the old world but in .NET anymore.

    When a Garbage Collection takes place the GC will detect that the specific variable isn't used anymore and will be Garbage Collected, so setting something to null/nothing has no effect anymore.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jul 2000
    Location
    India
    Posts
    22
    Hi,

    Ya, i tried unloading my own appdomain only not assembly.

    I created seperate appdomain, loaded my assembly in it, done work, unloaded that appdomain using AppDomain.Unload(myAppDomain) method.

    I doesn't work. Thanx for u'r info about setting to null.

    But, AppDomainSetup 's ShadowCompyFiles property set to "true" will solve our problem, but yet to check.


    Rafik

  5. #5
    Lively Member
    Join Date
    Aug 1999
    Location
    Amsterdam
    Posts
    117
    Take a look at the below code how to do it:

    public static void Main(string[] args)
    {
    AppDomain appDomain = AppDomain.CreateDomain("NewAppDomain");
    appDomain.DoCallBack(new CrossAppDomainDelegate(LoadAssembly));

    Console.WriteLine("Try to delete Assembly now"); // Delete/Overwrite will fail now
    Console.ReadLine();

    AppDomain.Unload(appDomain);
    Console.WriteLine("AppDomain unloaded");

    Console.WriteLine("Try to delete Assembly now"); // Delete/Overwrite wouldn't fail now
    Console.ReadLine();
    }

    public static void LoadAssembly()
    {
    Assembly assembly = Assembly.LoadFrom(@"c:\MyClassLibrary.dll");
    Console.WriteLine("Assembly loaded in {0}",AppDomain.CurrentDomain.FriendlyName);
    }

    BTW:
    ShadowCoping makes a copy of the original Assemblies specified in AppDomain.ShadowCopyDirectories and loads the copy, so the copy gets locked not the original ones.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width