Summary:
How to combine an injection module with regular contain.Resolve()
calls in registration?
~ Update (5 hours later and after feedback)~ :
The use of RegisterType as opposed в Регистр () не решает реальный уровень сложности у меня в производственный код, я обновил демонстрационный код на BitBucket, чтобы включить общий ISecond , с примерами: IMagic и CreateMagic (интерфейс, класс соответственно).
Код репозитория HG был обновлен, так как has the PasteBin link below.
Details:
There is a class with 2 dependencies
public SomeClassWithILogDependency(ILog log, ISecond second)
'ILog' has an InjectionModule defined for it such as, just like in the documentation
( LogforIntegration ) see bottom of post
If the class has only 1 dependency then this line of Autofac registration works in the simplest scenario:
builder.RegisterType()
.As()
.PropertiesAutowired();
BUT
I need to call container.Resolve
in the registration for more parameters on the class, but something like this does not work:
builder.Register(
c => new SomeClassWithILogDependency(
c.Resolve(), c.Resolve>()))
.As()
.PropertiesAutowired(); //with or without this
The c.Resolve
is the issue as "that service has not been registered."
I also tried
SomeClassWithILogDependency(c.ResolveOptional() //without luck...
Any ideas as to what I have missed, or completely different strategy? I hope the solution is not to have more InjectionModules...
Full code posted on PasteBin.com as a single file
Or entire solution from bitbucket.org
hg clone https://NickJosevski@bitbucket.org/NickJosevski/autofaclog4netdemo
Injection Module Code (to make this post complete):
public class LogInjectionModule : Module
{
protected override void AttachToComponentRegistration(
IComponentRegistry registry, IComponentRegistration registration)
{
registration.Preparing += OnComponentPreparing;
}
private static void OnComponentPreparing(object sender, PreparingEventArgs e)
{
var t = e.Component.Activator.LimitType;
e.Parameters = e.Parameters.Union(new[]
{
new ResolvedParameter(
(p, i) => p.ParameterType == typeof (ILog),
(p, i) => LogManager.GetLogger(t))
});
}
}