Castle Windsor XML config not finding type
Asked Answered
M

0

6

I normally register components using (fluent) code but I now have a requirement to register certain types via XML. The file looks like this:-

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <components>
    <component service="MyNamespace.IFoo, MyAssembly"
               type="MyNamespace2.ConcreteFoo, MyAssembly2"
               lifestyle="transient" />
  </components>
</configuration>

And I use the following code to register the types:-

container.Install(
    Castle.Windsor.Installer.Configuration.FromXmlFile(filename));

However this falls over with a Castle.MicroKernel.SubSystems.Conversion.ConverterException:-

Could not convert string 'MyNamespace2.ConcreteFoo, MyAssembly2' to a type. Assembly MyAssembly2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null was matched, but it doesn't contain the type. Make sure that the type name was not mistyped.

To make sure I wasn't going mad I added this code just before the above line:-

var test = AppDomain.CurrentDomain.GetAssemblies();

A breakpoint confirms that the assembly in question is in the current AppDomain, and the concrete type in question is in this assembly's 'DefinedTypes'. It's also public.

This should be a straightforward thing to do, but I'm scratching my head now!

Edit

I should point out that the assembly containing the concrete type is loaded at runtime (using MEF), and exists in a different folder to the application's exe (so I can't use <using> in my XML file). However I had assumed that as it's present in the AppDomain then what I'm doing should work. Or is Windsor doing something strange like trying to load the assembly from file again, rather than just looking in the AppDomain?

Edit 2

As an experiment I've just tried this line of code:-

container.Register(Classes.FromAssemblyNamed("MyAssembly2").BasedOn<IFoo>());

This throws a FileNotFoundException, backing up my earlier hunch that Windsor is trying to load the assembly from file rather than checking in the AppDomain. Why is this?

Mongoose answered 20/4, 2016 at 12:48 Comment(5)
Looking at the code, one quick question jumps out: does your assembly's actual name start with "System"? It looks like it'd be skipped if so.Hoban
@PatrickQuirk noticed that myself after a quick look at the Windsor source, but no it doesn't.Mongoose
That would have been too easy :) At this point if it were me, I'd fire up dotPeek to generate symbols for the library and start walking through the Windsor code.Hoban
You didn't post the definition of class ConcreteFoo. Is it public?Pentlandite
@Pentlandite yes it is publicMongoose

© 2022 - 2024 — McMap. All rights reserved.