Castle Windsor - FileNotFoundException in Unit Tests

Viewed 63

I’ve inherited an ASP.Net web application comprising of VB.Net projects. I’ve added a C# project to the solution, which references several of the VB.Net projects.

The application uses Castle Windsor as a DI container and dependencies are registered in one of the VB.Net projects.

In that VB.Net project. to avoid circular references, I use the “FromAssemblyNamed”, method to register the dependencies that are defined in my C# project:

container.Register(Classes.FromAssemblyNamed("x.y.z.TLBusinessLayerV2").BasedOn(Of IService).WithService.FromInterface().Configure(Function(c) c.LifeStyle.Transient))

This is working fine when I run the application.

However, there is an existing Unit Test project. In that I have added a reference to my C# assembly. When I run all of the tests, some of the tests fail when they hit the code above with the following exception:

System.IO.FileNotFoundException: Could not load file or assembly x.y.z.TLBusinessLayerV2' or one of its dependencies. The system cannot find the file specified.

  • All of the assemblies referenced by the C# Assembly are also referenced by the Unit Test project.
  • The Target CPU for the VB.Net project, C# project and Unit Test project are all set to x86.

If I then run one of the failed tests individually, the test passes.

Q. Why are the Tests failing when I run them all? Any help much appreciated.

1 Answers

The solution to this issue is to add a dummy class to the Unit Test project, that references a class in the assembly (loaded in "Classes.FromAssemblyNamed"). Something like:

Public Class DummyDependencies
        Private Xyz As Object = GetType(<name of a class in target assembly>)
End Class

This appears to enforce the reference to the assembly.

Related