[英]AutoFac: NullReferenceException on Container Build
I am using AutoFac 4.6.0 with XML Configuration File and Modules in an Web API / OWIN Context. 我在Web API / OWIN上下文中使用带有XML配置文件和模块的AutoFac 4.6.0。
When I call Build() of ContainerBuilder, I am getting a NullReferenceException. 当我调用ContainerBuilder的Build()时,我收到了NullReferenceException。 What am I doing wrong? 我究竟做错了什么? Is it, because "TheFailingModule" is in another assembly? 是因为“ TheFailingModule”在另一个程序集中吗? If I add the property "Foo" to "TestModule", it works. 如果我将属性“ Foo”添加到“ TestModule”,它将起作用。
I added a break point in the Load-Methods of the modules, theyhave not been hitted. 我在模块的“加载方法”中添加了一个断点,但尚未实现。
UPDATE 更新
I found out, that the private property MyAppContext causes the issue. 我发现,私有属性MyAppContext导致了此问题。
Please see the code: 请查看代码:
Startup.cs 启动文件
namespace MyNamespace.Web
{
// [...]
public void Configuration(IAppBuilder app)
{
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddXmlFile("autofac.config");
var module = new ConfigurationModule(configurationBuilder.Build());
var builder = new ContainerBuilder();
builder.RegisterModule(module);
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
var config = new HttpConfiguration();
builder.RegisterWebApiFilterProvider(config);
AutoFacConfig.Register(builder);
var container = builder.Build(); // <-- NullReferenceException
// [...]
}
// [...]
}
autofac.config autofac.config
<?xml version="1.0" encoding="utf-8"?>
<autofac>
<modules name="TestModule">
<type>MyNamespace.Web.TestModule, MyNamespace.Web</type>
</modules>
<modules name="TheFailingModule">
<type>MyNamespace.Failing.TheFailingModule, MyNamespace.Failing</type>
<properties Foo="Bar"></properties>
</modules>
</autofac>
TheFailingModule.cs TheFailingModule.cs
namespace MyNamespace.Failing
{
public class TheFailingModule : Module
{
private MyAppContext MyAppContext { get; set; }
public string Foo { get; set; }
protected override void Load(ContainerBuilder builder)
{
// [...]
base.Load(builder);
}
}
}
TestModule 测试模块
namespace MyNamespace.Web
{
public class TestModule : Module
{
protected override void Load(ContainerBuilder builder)
{
// Do some stuff
base.Load(builder);
}
}
}
StackTrace 堆栈跟踪
at Autofac.Configuration.Core.ConfigurationExtensions.<>c__DisplayClass3_0.<GetProperties>b__0(ParameterInfo pi, IComponentContext c)
at Autofac.Core.ResolvedParameter.CanSupplyValue(ParameterInfo pi, IComponentContext context, Func`1& valueProvider)
at Autofac.Core.Activators.Reflection.ReflectionActivator.InjectProperties(Object instance, IComponentContext context)
at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)
at Autofac.Configuration.Core.ModuleRegistrar.RegisterConfiguredModules(ContainerBuilder builder, IConfiguration configuration)
at Autofac.Configuration.Core.ConfigurationRegistrar.RegisterConfiguration(ContainerBuilder builder, IConfiguration configuration)
at Autofac.Configuration.ConfigurationModule.Load(ContainerBuilder builder)
at Autofac.Module.Configure(IComponentRegistry componentRegistry)
at Autofac.ContainerBuilder.Build(IComponentRegistry componentRegistry, Boolean excludeDefaultModules)
at Autofac.ContainerBuilder.Build(ContainerBuildOptions options)
I found the issue. 我发现了问题。 I used a private property in the module (MyAppContext) whicht was not set by autofac.config. 我在模块(MyAppContext)中使用了一个私有属性,该属性不是由autofac.config设置的。 I changed it to a private field and is now working. 我将其更改为私有字段,现在可以使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.