When registering a type to be constructed by a factory method, it is sometimes important to know what type the instance is being constructed for.
For example, when using log4net you might want to inject a logger for that specific type.
I knew how to do it using StructureMap:
For<ILog>().Use(s => LogManager.GetLogger(s.Root.ConcreteType));
But today I had to work out how to do it with Castle. (It’s pretty similar).
Component.For<ILog>().UsingFactoryMethod((k, cm, cc) => LogManager.GetLogger(cc.Handler.Service))
Most error messages are cryptic at best, so coming across this pearl from Castle Windsor was a breath of fresh air!
System.Exception: Looks like you forgot to register the http module Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule Add '<add name="PerRequestLifestyle" type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.Windsor" />' to the <httpModules> section on your web.config. If you're running IIS7 in Integrated Mode you will need to add it to <modules> section under <system.webServer> at Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleManager.Resolve(CreationContext context)
Not only does it tell you what is wrong, but also how to fix it. Kudos to the Castle team.
UPDATE: Now with even more detail!