2
votes

Windsor castle cannot Resolve any type after registering them by Assembly

I am trying implement a service that scans the current library for all '.ddl' extension and then registering them by Assembly in Castle Windsor, but when i try to resolve An exception is thrown with the message : 'No component for supporting the service'. If i iterate through Kernel.GetAssignableHandlers i can see that all types are registered.

1. get the assembly root name:

var delimiterIndex = assembly.FullName.IndexOfAny(new[] { '.' });

2. Get all the Assemblies as an Array

return Directory.EnumerateFiles(AppDomain.CurrentDomain.BaseDirectory, "*.*", SearchOption.AllDirectories)
                       .Where(x => Path.GetExtension(x).Equals(".dll", StringComparison.InvariantCultureIgnoreCase))
                         .Where(x => Path.GetFileName(x)
                            .StartsWith(rootAssemblyName, StringComparison.InvariantCultureIgnoreCase)).Select(Assembly.LoadFile).ToArray();

3. Register each Assembly in Windsor Castle Container

Classes.FromAssembly(assembly).Pick().WithServiceBase().WithServiceAllInterfaces().WithServiceSelf().LifestyleTransient()

4. Resolve

var instance = _container.Resolve<IFoo>();

When Resolving 'ComponentNotFoundException' is thrown with the message:

'No component for supporting the service Mes.Utils.DBUpgrade.IDBUpgrader was found'

1
I would rather don't use this approach (locate implementation via searching through many dlls), instead I would register concrete implementation per interface. If it's difficult to implement, imagine problems to troubleshoot production issues, should such take place.Renat
Is it possible that you've registered all of the types that you directly need to work with, but one of those depends on IDBUpgrader which isn't registered? Are you able to resolve other types from the assembly?Scott Hannen
You make an Excellent point. I tried running and ResolveAll(typeOf(object)) which threw an unsatisfied exception on a Custom Exception, due to a string argument in the constructor. Excluding the Expception type from the registration allows me to call a Resolve all on type of object succesfully, and if iterate through the returned array and call GetType(), it is the correct types that are returned, but if i try a single resolve, it still fails. If there are unsatified Dependencies, wouldn't the exception be thrown upwards?Martin
The assembly array is filled, through the debugger in can see that the container have the types registered, and have no unsatisfied dependencies. But i think that i will give Classes.FromAssemblyInDirectory() a try, to be honest i missed that one in the documentationMartin

1 Answers

1
votes

I never manage to find the actual error in the actual code, but Krzysztof Kozmic suggested to use Classes.FromAssemblyNamed instead of Classes.FromAssembly which works great :D

so the change made to the original code compare to that showed in section 3 is as shown : Classes.FromAssemblyNamed(assembly.FullName).Pick().WithServiceBase().WithServiceAllInterfaces().WithServiceSelf().LifestyleTransient().

i guess that the problem somehow was the way i loaded the assemblies, but castle windsor manage to load them probably

Thank you all the answers and discussion in the comments :D