light inject - LightInject IoC container throws stackoverflow in fallback method -
i fiddling lightinject try , set ioc solution containing domain proj, infrastructure proj, mvc proj, , dependencyresolution proj. infrastructure, mvc, , dependencyresolution references domain. mvc references dependencyresolution , dependencyresolution references lightinject.
the idea dependencyresolution registers necessary dependencies @ app startup. has no knowledge of controllers in mvc proj @ time. instead set fallback routine catch unknown mvc controller classes. in fallback routine register mvc controller , return it. doing expect code run 1 time, since first hit mvc controller not registered yet. not case. instead stackoverflowexception because fallback routine hit every time mvc controller asked though registered first time.
so question why happens? expected behaviour , if so, why , how around it?
edit: here source code below.
[assembly: webactivatorex.preapplicationstartmethod(typeof(dependencyresolution.app_start.webcommon), "start")] namespace dependencyresolution.app_start { static class webcommon { private static readonly tempproject.lightinject.servicecontainer _servicecontainer = new tempproject.lightinject.servicecontainer(); /// <summary> /// starts application /// </summary> public static void start() { var container = _servicecontainer; registerservices(container); domain.servicelocator.setservicelocator(() => new servicelocator(container)); } /// <summary> /// load modules or register services here! /// </summary> /// <param name="container">the ioc container.</param> private static void registerservices(tempproject.lightinject.servicecontainer container) { system.func<tempproject.lightinject.servicerequest, object> fallback = request => { var servicetype = request.servicetype; container.register(servicetype, servicetype); return container.getinstance(servicetype); }; container.registerfallback((type, s) => type.name.endswith("controller"), request => fallback(request)); var assembly = typeof(domain.iservicelocator).assembly; container.registerassembly(assembly); } } }
the registerfallback
method expects factory second parameter , not seem register type in such way container can resolve it.
your factory needs new instance , pass back.
func<servicerequest, object> fallback = request => { if (request.servicetype == typeof(xyzcontroller)) { return new xyzcontroller(container.getinstance<idependency>()); } else { throw new invalidoperationexception(request.servicetype.fullname); } };
this might issue worth raising developers.
you load mvc assemblies using code this:
private ienumerable<assembly> loadassemblies(string folder) { var dlls = file in new directoryinfo(folder).getfiles() file.extension == ".dll" select file.fullname; var assemblies = new list<assembly>(); foreach (string dll in dlls) { try { assemblies.add(assembly.loadfile(dll)); } catch { } } return assemblies; }
and register controllers lightinject mvc integration package
Comments
Post a Comment