I have a test that relies on deserializing data via a call to BinaryFormatter.Deserialize(). This method relies on the availability of a type definied in an assembly referenced from the assembly making the call. While this code works perfectly by turning
my test assembly into a console application and calling my test from void Main(...), it fails when called via nBehave-Console.exe with the following.
System.Runtime.Serialization.SerializationException : Unable to find assembly 'X.Y.Z, Version=188.8.131.52, Culture=neutral, PublicKeyToken=null'.
System.NullReferenceException : Object reference not set to an instance of an object.
at Y.Tests.Z.Load() in C:\Dev\X\Core\Trunk\src\Y\Y.Tests
By turning on Fusion logging we also get
*** Assembly Binder Log Entry (29/07/2011 @ 12:51:08) ***
The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.
Assembly manager loaded from: C:\WINNT\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable C:\Program Files\NBehave\0.5.0.252\v4.0\NBehave-Console.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: User = X\agardne5
LOG: DisplayName = X.Y.Z, Version=184.108.40.206, Culture=neutral, PublicKeyToken=null
LOG: Appbase = file:///C:/Program Files/NBehave/0.5.0.252/v4.0/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = NBehave-Console.exe
Calling assembly : (Unknown).
LOG: This bind starts in default load context.
LOG: No application configuration file found.
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\WINNT\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
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/NBehave/0.5.0.252/v4.0/X.Y.Z.DLL.
LOG: Attempting download of new URL file:///C:/Program Files/NBehave/0.5.0.252/v4.0/X.Y.Z/X.Y.Z.DLL.
LOG: Attempting download of new URL file:///C:/Program Files/NBehave/0.5.0.252/v4.0/X.Y.Z.EXE.
LOG: Attempting download of new URL file:///C:/Program Files/NBehave/0.5.0.252/v4.0/X.Y.Z/X.Y.Z.EXE.
LOG: All probing URLs attempted and failed.
This implies to me it might be worth spinning up the test code in a new AppDomain with a new AppBase of the Test.dll's current directory.
In fact my hack is to add the following.
[When("I try to load")]
public void Load()
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
var dlls = Directory.GetFiles(Directory.GetCurrentDirectory(), args.Name + ".dll");
if (dlls.FirstOrDefault() != null)
Console.WriteLine("Redirecting assemblyLoad" + dlls.First());
var exes = Directory.GetFiles(Directory.GetCurrentDirectory(), args.Name + ".exe");
if (exes.FirstOrDefault() != null)
Console.WriteLine("Redirecting assemblyLoad" + exes.First());
Is there any reason this is by design?