Autofac没有发现构造函数

时间:2018-07-07 13:09:17

标签: c# .net winforms dependency-injection autofac

我使用Autofac使用DependencyInjection创建一个项目WindowsForm应用程序。构建它时出现问题。这是我的Program.cs

        var builder = new ContainerBuilder();
        builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly());
        builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerDependency();
        builder.RegisterType<DbFactory>().As<IDbFactory>().InstancePerDependency();
        builder.RegisterType<DITestDbContext>().AsSelf().InstancePerDependency();
        // Repositories
        builder.RegisterAssemblyTypes(typeof(ProductCategoryRepository).Assembly)
            .Where(t => t.Name.EndsWith("Repository"))
            .AsImplementedInterfaces().InstancePerDependency();

        // Services
        builder.RegisterAssemblyTypes(typeof(ProductCategoryService).Assembly)
           .Where(t => t.Name.EndsWith("Service"))
           .AsImplementedInterfaces().InstancePerDependency();
        Autofac.IContainer container = builder.Build();

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(container.Resolve<Form1>());

我遇到错误消息

  

DependencyResolutionException:在激活特定注册期间发生错误。有关详细信息,请参见内部异常。注册:激活程序= ProductCategoryService(ReflectionActivator),服务= [DITest.Service.IProductCategoryService],生命周期= Autofac.Core.Lifetime.CurrentScopeLifetime,共享=无,所有权= OwnedByLifetimeScope

  

DependencyResolutionException:在激活特定注册期间发生错误。有关详细信息,请参见内部异常。注册:激活程序= UnitOfWork(ReflectionActivator),服务= [DITest.Data.Infrastructure.IUnitOfWork],生命周期= Autofac.Core.Lifetime.CurrentScopeLifetime,共享=无,所有权= OwnedByLifetimeScope

还有

  

NoConstructorsFoundException:找不到类型为'DITest.Data.Infrastructure.UnitOfWork'的可访问构造函数。

任何人都知道如何解决此问题。谢谢!!

1 个答案:

答案 0 :(得分:1)

UnitOfWork可能没有公共构造函数,但您没有为此发布代码。