I can't modify a dll even though its AppDomain has been unloaded
-
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 -
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 -
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, 2006Check 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.
-
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, 2006AppDomain.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.
-
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.
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.
-
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.