Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. I can't modify a dll even though its AppDomain has been unloaded

I can't modify a dll even though its AppDomain has been unloaded

Scheduled Pinned Locked Moved C#
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kayhustle
    wrote on last edited by
    #1

    I needed to access an assembly, which I did by loading it into another AppDomain. Next, I unload the AppDomain. Now when I try to open up that dll for writing it tells me that the file is being used by another process. I thought that the file got unloaded once the AppDomain was unloaded. That is the only reason I loaded the dll into another AppDomain, so that I could unload it and allow it to be overwritten afterward. Here is my code. The dll is in the directory that the main executable is running in. This is the only way I got the domain to load the assembly. Once the domain loads the assembly a FileStream cannot be opened for writing on that file. It makes no sense because I unload the domain :~ : AppDomainSetup aps = new AppDomainSetup(); aps.ApplicationBase=@"myapplocation"; AppDomain domain = AppDomain.CreateDomain("My New Domain",null,aps); string apploc = @"myexecuting assembly location"; domain.ExecuteAssembly(apploc); Assembly ass = domain.Load("AdCenter"); AppDomain.Unload(domain); //The following call throws an exception saying that the file is in use by another process FileStream fs = new FileStream("AdCenter.dll", FileMode.Open, FileAccess.ReadWrite); Thanks -- modified at 15:59 Thursday 30th March, 2006

    E L D 3 Replies Last reply
    0
    • K kayhustle

      I needed to access an assembly, which I did by loading it into another AppDomain. Next, I unload the AppDomain. Now when I try to open up that dll for writing it tells me that the file is being used by another process. I thought that the file got unloaded once the AppDomain was unloaded. That is the only reason I loaded the dll into another AppDomain, so that I could unload it and allow it to be overwritten afterward. Here is my code. The dll is in the directory that the main executable is running in. This is the only way I got the domain to load the assembly. Once the domain loads the assembly a FileStream cannot be opened for writing on that file. It makes no sense because I unload the domain :~ : AppDomainSetup aps = new AppDomainSetup(); aps.ApplicationBase=@"myapplocation"; AppDomain domain = AppDomain.CreateDomain("My New Domain",null,aps); string apploc = @"myexecuting assembly location"; domain.ExecuteAssembly(apploc); Assembly ass = domain.Load("AdCenter"); AppDomain.Unload(domain); //The following call throws an exception saying that the file is in use by another process FileStream fs = new FileStream("AdCenter.dll", FileMode.Open, FileAccess.ReadWrite); Thanks -- modified at 15:59 Thursday 30th March, 2006

      E Offline
      E Offline
      Ed Poore
      wrote on last edited by
      #2

      I don't think that there's any guarantee that the domain has been unloaded because the GC could still retain a reference to it. Or then again maybe I'm confusing it with something else, although it's still worth checking up on. Ed

      1 Reply Last reply
      0
      • K kayhustle

        I needed to access an assembly, which I did by loading it into another AppDomain. Next, I unload the AppDomain. Now when I try to open up that dll for writing it tells me that the file is being used by another process. I thought that the file got unloaded once the AppDomain was unloaded. That is the only reason I loaded the dll into another AppDomain, so that I could unload it and allow it to be overwritten afterward. Here is my code. The dll is in the directory that the main executable is running in. This is the only way I got the domain to load the assembly. Once the domain loads the assembly a FileStream cannot be opened for writing on that file. It makes no sense because I unload the domain :~ : AppDomainSetup aps = new AppDomainSetup(); aps.ApplicationBase=@"myapplocation"; AppDomain domain = AppDomain.CreateDomain("My New Domain",null,aps); string apploc = @"myexecuting assembly location"; domain.ExecuteAssembly(apploc); Assembly ass = domain.Load("AdCenter"); AppDomain.Unload(domain); //The following call throws an exception saying that the file is in use by another process FileStream fs = new FileStream("AdCenter.dll", FileMode.Open, FileAccess.ReadWrite); Thanks -- modified at 15:59 Thursday 30th March, 2006

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        Check the VS.NET output window, when the 'apploc' assembly loads. What is happening most likely that one of the Types in AdCenter.dll is crossing the domain boundry and effectively locking it. If that is the case, you will have to be a bit more clever how you execute the additional appdomain, it will depend on what your code does, so I cant help you with that.

        xacc.ide-0.1.3.2

        1 Reply Last reply
        0
        • K kayhustle

          I needed to access an assembly, which I did by loading it into another AppDomain. Next, I unload the AppDomain. Now when I try to open up that dll for writing it tells me that the file is being used by another process. I thought that the file got unloaded once the AppDomain was unloaded. That is the only reason I loaded the dll into another AppDomain, so that I could unload it and allow it to be overwritten afterward. Here is my code. The dll is in the directory that the main executable is running in. This is the only way I got the domain to load the assembly. Once the domain loads the assembly a FileStream cannot be opened for writing on that file. It makes no sense because I unload the domain :~ : AppDomainSetup aps = new AppDomainSetup(); aps.ApplicationBase=@"myapplocation"; AppDomain domain = AppDomain.CreateDomain("My New Domain",null,aps); string apploc = @"myexecuting assembly location"; domain.ExecuteAssembly(apploc); Assembly ass = domain.Load("AdCenter"); AppDomain.Unload(domain); //The following call throws an exception saying that the file is in use by another process FileStream fs = new FileStream("AdCenter.dll", FileMode.Open, FileAccess.ReadWrite); Thanks -- modified at 15:59 Thursday 30th March, 2006

          D Offline
          D Offline
          Daniel Grunwald
          wrote on last edited by
          #4

          AppDomain.Load will load the assembly BOTH in the target domain AND current domain. AppDomain.Load is for calls through COM from unmanaged code only. Use AppDomain.CreateInstanceAndUnwrap to create an instance of a MarshalByRef class in your main assembly in the target domain. That class then can use Assembly.Load to load additional assemblies that should be active only in the target AppDomain.

          K 1 Reply Last reply
          0
          • D Daniel Grunwald

            AppDomain.Load will load the assembly BOTH in the target domain AND current domain. AppDomain.Load is for calls through COM from unmanaged code only. Use AppDomain.CreateInstanceAndUnwrap to create an instance of a MarshalByRef class in your main assembly in the target domain. That class then can use Assembly.Load to load additional assemblies that should be active only in the target AppDomain.

            K Offline
            K Offline
            kayhustle
            wrote on last edited by
            #5

            I'm not sure I understand what you're saying. First off, AppDomain.CreateInstanceAndUnwrap loads the assembly into the both domains just as AppDomain.Load does. Also I don't get how I would be able to use that class to call Assembly.Load since the class that is returned is the class object that I instantiated.

            K 1 Reply Last reply
            0
            • K kayhustle

              I'm not sure I understand what you're saying. First off, AppDomain.CreateInstanceAndUnwrap loads the assembly into the both domains just as AppDomain.Load does. Also I don't get how I would be able to use that class to call Assembly.Load since the class that is returned is the class object that I instantiated.

              K Offline
              K Offline
              kayhustle
              wrote on last edited by
              #6

              Ok, I get what you're saying, if my class inherits MarshalByRefObjectthen I can load the assembly inside of that class. Since my class inherits MarshalByRefObject it get returned as a proxy.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups