1

Closed

Unable to find assembly when Deserializing

description

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=2.2.0.0, 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=2.2.0.0, Culture=neutral, PublicKeyToken=null
(Fully-specified)
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);
 
        //LoadSomethingAndCallBinaryFormatter.Deserialize
    }
 
    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());
            return Assembly.LoadFrom(dlls.First());
        }
        var exes = Directory.GetFiles(Directory.GetCurrentDirectory(), args.Name + ".exe");
        if (exes.FirstOrDefault() != null)
        {
            Console.WriteLine("Redirecting assemblyLoad" + exes.First());
            return Assembly.LoadFrom(exes.First());
        }
        return null;
    }
 
 
Is there any reason this is by design?
Closed Jul 30, 2011 at 11:17 AM by Lazydev
Its a bug, and if my repro is correct it has already been fixed in 0.5.1. You can get it from the download page (http://nbehave.codeplex.com/releases/view/68223)

comments