Autofac 未找到任何构造函数

hii*_*t95 -1 .net c# dependency-injection autofac winforms

我使用 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>());
Run Code Online (Sandbox Code Playgroud)

我遇到一条错误消息

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

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

NoConstructorsFoundException:找不到类型“DITest.Data.Infrastruct.UnitOfWork”的可访问构造函数。

有人知道怎么修这个东西吗。感谢您!!

Geo*_*yar 5

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

  • 当您甚至没有发布代码时,不要对试图帮助您的人投反对票。您的错误消息显示“找不到类型‘DITest.Data.Infrastruct.UnitOfWork’的可访问构造函数。” 这意味着没有公共构造函数,因此如果不发布您的代码,没有人能够给您任何更好的答案。 (6认同)