tags:

views:

7165

answers:

6

I have written the following simple test in trying to learn Castle Windsor's Fluent Interface:

using NUnit.Framework;
using Castle.Windsor;
using System.Collections;
using Castle.MicroKernel.Registration;

namespace WindsorSample {
    public class MyComponent : IMyComponent {
        public MyComponent(int start_at) {
            this.Value = start_at;
        }
        public int Value { get; private set; }
    } 
    public interface IMyComponent {
        int Value { get; }
    }

    [TestFixture]
    public class ConcreteImplFixture {
        [Test]
        public void ResolvingConcreteImplShouldInitialiseValue() {
            IWindsorContainer container = new WindsorContainer();
            container.Register(Component.For<IMyComponent>().ImplementedBy<MyComponent>().Parameters(Parameter.ForKey("start_at").Eq("1")));
            IMyComponent resolvedComp = container.Resolve<IMyComponent>();
            Assert.AreEqual(resolvedComp.Value, 1); 
        }
    }
}

When I execute the test through TestDriven.NET I get the following error:

System.TypeLoadException : Could not load type 'Castle.MicroKernel.Registration.IRegistration' from assembly 'Castle.MicroKernel, Version=1.0.3.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc'.
at WindsorSample.ConcreteImplFixture.ResolvingConcreteImplShouldInitialiseValue()

When I execute the test through the NUnit GUI I get:

WindsorSample.ConcreteImplFixture.ResolvingConcreteImplShouldInitialiseValue:
System.IO.FileNotFoundException : Could not load file or assembly 'Castle.Windsor, Version=1.0.3.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc' or one of its dependencies. The system cannot find the file specified.

If I open the Assembly that I am referencing in Reflector I can see its information is:

Castle.MicroKernel, Version=1.0.3.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc

and that it definitely contains Castle.MicroKernel.Registration.IRegistration

What could be going on?

I should mention that the binaries are taken from the latest build of Castle though I have never worked with nant so I didn't bother re-compiling from source and just took the files in the bin directory. I should also point out that my project compiles with no problem.

+5  A: 

Is the assembly in the GAC or any place the might be overriding the assembly that you think is being loaded? This is usually the result of an incorrect assembly being loaded, for me it means I usually have something in the GAC overriding the version I have in bin/Debug.

spoon16
I added the assemblies by browsing to the dll files, so the GAC shouldn't be entering into the equation. Also right click on the assembly->open in reflector from the solution explorer brings it up in the reflector with all the information that I would expect
George Mauer
It doesn't matter how you added it if an assembly with the same name/version exists in the GAC it will load that one.
spoon16
VS.NET will list the path to the assembly you select and reflector will open the right assembly but when the application executes the .NET runtime will load the GAC'd assembly.
spoon16
It has been a long time since I posted this issue but for the sake of storing knowledge it did indeed have something to do with overriding assemblies. Namely, I think I was using Rhino.Mocks which has its own version of castle compiled in. I had to change it so that my code was not referencing this
George Mauer
Awesome this just fixed my problem. I was getting the error Could not load type 'Microsoft.Data.Objects.EntityConfiguration`1' from assembly 'Microsoft.Data.Entity.Ctp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. So I just uninstalled EF CTP 4 and all worked. Cheers
Jake Scott
+1  A: 

Version=1.0.3.0 indicates Castle RC3, however the fluent interface was developed some months after the release of RC3. Therefore, it looks like you have a versioning problem. Maybe you have Castle RC3 registered in the GAC and it's using that one...

Mauricio Scheffer
Interesting, I got the latest build here: http://builds.castleproject.org/cruise/DownloadBuild.castle?number=956Its what they recommended on their forum. Also if that was the case I imagine that the project wouldn't compile at all. But this compiles no problem
George Mauer
As for having the version in the GAC, I probably do, but I did not add the reference from the GAC and using reflector on the reference indicates that it is the one I thought
George Mauer
Try removing the duplicate assembly from the GAC, I'm willing to be that is your issue.
spoon16
+2  A: 

If you have one project referencing another project (such as a 'Windows Application' type referencing a 'Class Library') and both have the same Assembly name, you'll get this error. You can either strongly name the referenced project or (even better) rename the assembly of the referencing project (under the 'Application' tab of project properties in VS).

AndrewS
I could kiss you man. I had no idea this was the case. Thanks for saving me hours and hours of work.
Epaga
A: 

I get this occasionally and it's always been down to have the assembly in the GAC

SteveC
A: 

Hey AndrewS thanks for the solution ...

+4  A: 

Sorry to dredge up an old answered question, but the solution to this for me was not mentioned above, so I thought I would add my answer to the long tail...

I ended up having an old reference to a class (an HttpHandler) in web.config that was no longer being used (and was no longer a valid reference). For some reason it was ignored while running in Studio (or maybe I have that class still accessible within my dev setup?) and so I only got this error once I tried deploying to IIS. I searched on the assembly name in web.config, removed the unused handler reference, then this error went away and everything works great. Hope this helps someone else.

bmoeskau