Heya all,
I'm currently developing an application that loads a dll that is dynamically compiled every now and then. To avoid restarting my application, I chose to use an appDomain so I can call AppDomain.Unload(appDomain); whenever the DLL is build again.
So far I've got the following code:
AppDomainSetup ads = new AppDomainSetup();
String fullPath = GetFullFileName(); //returns full filename
ads.ApplicationBase = Path.GetDirectoryName( fullPath );
ads.PrivateBinPath = Path.GetDirectoryName( fullPath );
AppDomain ad = AppDomain.CreateDomain( "myName", null, ads );
ad.Load( AssemblyName.GetAssemblyName( fullPath ) ); ///-> Throws FileNotFoundException!
//do some stuff here (reflection on the dll mostly)
AppDomain.Unload(ad);
This code actually runs inside a Visual Studio environment... (might make a difference ).
Here's my fusion log:
Assembly manager loaded from: C:\WINDOWS\Microsoft.NET
Running under executable C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\devenv.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: User = clemens
LOG: DisplayName = myAsm, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
(Fully-specified)
LOG: Appbase = file:///C:/Program Files/Microsoft Visual Studio 8/Common7/IDE/
LOG: Initial PrivatePath = NULL
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\devenv.exe.Config
LOG: Using machine configuration file from C:\WINDOWS\Microsoft.NET
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio 8/Common7/IDE/myAsm.dll.
LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio 8/Common7/IDE/myAsm/myAsm.dll.
LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio 8/Common7/IDE/PublicAssemblies
LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio 8/Common7/IDE/PublicAssemblies
LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio 8/Common7/IDE/PrivateAssemblies
LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio 8/Common7/IDE/PrivateAssemblies
LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio 8/Common7/IDE/myAsm.EXE.
LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio 8/Common7/IDE/myAsm/myAsm.EXE.
LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio 8/Common7/IDE/PublicAssemblies
LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio 8/Common7/IDE/PublicAssemblies
LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio 8/Common7/IDE/PrivateAssemblies
LOG: Attempting download of new URL file:///C:/Program Files/Microsoft Visual Studio 8/Common7/IDE/PrivateAssemblies
So the problem is, the assembly cannot be found ( ) although I'm quite sure I set the appdomain properties correctly and off course the assembly is in its place.
Any hints are greatly appreciated, since I already spent a couple of days on this.
Thanks,
Clemens

AppDomain.Load assembly file not found exception ?
JeffRozar
Hello
I have exactly the same problem .
Seems to be when loading seperate Application Domain when running an designer (in VS ) something works a litle different.
And yes for me too Assembly Resolve of secondary application domain does not trigerred.
Did you tried the shadow copy files . I believe this time assembly must be unloaded when secondary application domains unloads...
Mybe this time works .....
If its works notify me too please
DISTEF
Odin_dark
Good to hear someone actually has the same problem! I'm trying to figure it out through all kinds of ways, but no progress yet. Could you please report back any progress on your side as well
Thanks!!
Dot Dot Dog
It is clearly probing the wrong folder for the assembly. Do you have the hosting process enabled (Project + properties, Debug tab) What folder is "fullPath" pointing to
SriniX
I also have the same problem. Could you be a bit more specific about the solution you found Perhaps throw in some code too Thanx a lot in advance...
Martin Hart Turner
Heya.. I figured it out... It's not easy though... you have to use .NET Remoting on this. MarshalByRefObject and use CreateInstanceAndUnwrap on your appDomain. You can pass data from the MarshalByRefObject implementer to your current appdomain. But this cannot be type information, just strings/ints etc.. (by value! not by reference) .. and you can also pass serializable objects you created yourself or simple arrays.
WXS123
Thanks for your reply. I've already tried this, but the ad_AssemblyResolve never gets called (dropped a breakpoint in there). Any reason
Code:
public void myDynamicLoad()
{
AppDomainSetup ads = new AppDomainSetup(); String fullPath = GetAssemblyFileName();ads.ApplicationBase =
Path.GetDirectoryName( fullPath );ads.PrivateBinPath =
Path.GetDirectoryName( fullPath ); AppDomain ad = AppDomain.CreateDomain( "myName", null, ads );ad.AssemblyResolve +=
new ResolveEventHandler( ad_AssemblyResolve ); try{
ad.Load(
AssemblyName.GetAssemblyName( fullPath ) );}
catch( Exception e ){
MessageBox.Show( e.Message );}
}
static Assembly ad_AssemblyResolve( object sender, ResolveEventArgs args ){
return Assembly.LoadFile( GetAssemblyFileName() );}
tonyerfer
Sergey Zwezdin
Thanks again, really appreciate you putting effort in this!
"Enable the visual studio hosting process" is greyed out. Probably because my external program to start (and load) .dll is:
C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\\devenv.exe
(with command line arguments: /rootsuffix Exp /DesignTimeRun )
The fullPath is most certainly pointing to the right directory. It works with Assembly.LoadFile( fullPath ); just not in the seperate appDomain.
---------
ah, if I change the appDomain to CurrentDomain (this line of code):
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler( ad_AssemblyResolve );Then the assembly gets loaded.
HOWever, I'm not able to remove the assembly at runtime when I unload the domain, for its handle is still open ( ) probably because it is loaded in the CurrentDomain as well as in my own appDomain
Any thoughts on that as well
Greets,
Clemens