Autofac injects null
I have a very strange problem with Autofac 2.5.2.830 for .NET 3.5:
I user builder.RegisterAssemblyTypes
to register the types in my assemblies:
builder.RegisterAssemblyTypes("My.*.dll").Where(x => x.Name.EndsWith("Service"))
.AsImplementedInterfaces();
I have several classes that have a dependency on IConfigService
. A instance of the concrete implementor of this interface is injected to them but on class gets null
injected.
null
injected, sometimes the null
is injected even with the breakpoint. Setting a break point in one of the classes that are created earlier and also have a dependency on IConfigService
leads to an injection of a value instead of n开发者_JAVA百科ull
into the problematic class. Even removing a completely unrelated registration from the builder caused the bug to go away once.
However, one thing seems to be constant: It is always the same class that receives the null
.
I have no idea where to begin troubleshooting this problem. Is something like this known? What might be the reasons for a null
injection?
UPDATE
Oh my, this is stupid...The constructor looks like this:
public EmailNotifier(IConfigurationService configService)
{
_mailServerAddress = _configService.Get(Resources.MailServerAddress,
Resources.DefaultSenderAddress);
_configService = configService;
}
When run, this gave me a NullReferenceException
in the first line of the constructor, because _configService
was null
. It looks like, when I set the breakpoint, I sometimes checked whether configService
is null
and sometimes whether _configService
is null. Because of this it looked like sometimes I got null
injected and sometimes not.
SUMMARY
This is NOT a problem with Autofac, becausenull
was never injected. configService
always had a value.Can you post some code for "the same class that receives the null." - especially the constructor(s)
Can you also confirm that the null can definitely be seen if you put a breakpoint in the constructor - not that it could be some overwritten somewhere later?
My guess is that something is going wrong in the class somewhere.
精彩评论