Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# AutoFac:容器生成时出现NullReferenceException_C#_Autofac - Fatal编程技术网

C# AutoFac:容器生成时出现NullReferenceException

C# AutoFac:容器生成时出现NullReferenceException,c#,autofac,C#,Autofac,我在Web API/OWIN上下文中使用带有XML配置文件和模块的AutoFac 4.6.0 当我调用ContainerBuilder的Build()时,我得到一个NullReferenceException。 我做错了什么?是不是因为“故障模块”在另一个程序集中?如果我将属性“Foo”添加到“TestModule”,它就会工作 我在模块的加载方法中添加了一个断点,它们没有被命中 更新 我发现,私有属性MyAppContext导致了问题 请参阅代码: Startup.cs namespace M

我在Web API/OWIN上下文中使用带有XML配置文件和模块的AutoFac 4.6.0

当我调用ContainerBuilder的Build()时,我得到一个NullReferenceException。 我做错了什么?是不是因为“故障模块”在另一个程序集中?如果我将属性“Foo”添加到“TestModule”,它就会工作

我在模块的加载方法中添加了一个断点,它们没有被命中

更新

我发现,私有属性MyAppContext导致了问题

请参阅代码:

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
        // [...]
    }
    // [...]
}
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);
        }
    }
}
测试模块

namespace MyNamespace.Web
{
    public class TestModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            // Do some stuff

            base.Load(builder);
        }
    }
}
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)
StackTrace

namespace MyNamespace.Web
{
    public class TestModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            // Do some stuff

            base.Load(builder);
        }
    }
}
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)
在Autofac.Configuration.Core.ConfigurationExtensions.c_uuuDisplayClass3_0.b_uu0(ParameterInfo pi,IComponentContext c)
在Autofac.Core.ResolvedParameter.CanSupplyValue(ParameterInfo pi、IComponentContext上下文、Func`1和valueProvider)
位于Autofac.Core.Activators.Reflection.ReflectionActivator.InjectProperties(对象实例,IComponentContext上下文)
在Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext上下文,IEnumerable`1参数)中
在Autofac.Configuration.Core.ModuleRegistrar.RegisterConfiguredModules(ContainerBuilder生成器,IConfiguration配置)中
在Autofac.Configuration.Core.configurationRegistrator.RegisterConfiguration(ContainerBuilder构建器,IConfiguration配置)中
在Autofac.Configuration.ConfigurationModule.Load(ContainerBuilder生成器)处
在Autofac.Module.Configure(IComponentRegistry componentRegistry)处
在Autofac.ContainerBuilder.Build(IComponentRegistry componentRegistry,布尔ExcludeDefaultModule)上
在Autofac.ContainerBuilder.Build上(ContainerBuildOptions)
我发现了问题。 我在模块(MyAppContext)中使用了一个私有属性,该属性不是由autofac.config设置的。我将其更改为私有字段,现在正在工作。

我发现了问题。
我在模块(MyAppContext)中使用了一个私有属性,该属性不是由autofac.config设置的。我将其更改为一个私有字段,现在正在工作。

TestModule的代码是什么?将Foo添加到TestModule的想法从何而来?这些课程相关吗?@Uwe请查看我的更新。我刚刚将Foo添加到TestModule中,因为我想测试它是否能与这个模块一起工作。这些类没有关联。顺便说一句:加载方法没有被调用,所以异常发生在前面。TestModule的代码是什么?将Foo添加到TestModule的想法从何而来?这些课程相关吗?@Uwe请查看我的更新。我刚刚将Foo添加到TestModule中,因为我想测试它是否能与这个模块一起工作。这些类没有关联。顺便说一句:没有调用的Load方法,因此异常会更早发生。