Click here to Skip to main content
15,883,835 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have problem when I build project WindowsForm application Dependency Injection. Here this is my code in Program.cs file.
var builder = new ContainerBuilder();
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly());
// Register your Web API controllers.
//builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerDependency();
builder.RegisterType<DbFactory>().As<IDbFactory>().InstancePerDependency();

builder.RegisterType<DITestDbContext>().AsSelf().InstancePerDependency();
//builder.Register(c => app.GetDataProtectionProvider()).InstancePerRequest();

// 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>());


And here this is my code in Form1.cs
private IProductCategoryService productCategoryService;
private IUnitOfWork unitOfWork;
public Form1(IProductCategoryService productCategoryService, IUnitOfWork unitOfWork)
{
   this.productCategoryService = productCategoryService;
   this.unitOfWork = unitOfWork;
   InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
   base.OnLoad(e);
   LoadProductCategory();
}

private void LoadProductCategory()
{
   var data = productCategoryService.GetAll();
   gridControl1.DataSource = data;
}


And I get the error

Quote:
'An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = Form1 (ReflectionActivator), Services = [Presentation.Form1], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = OwnedByLifetimeScope'

Quote:
NoConstructorsFoundException: No accessible constructors were found for the type 'DITest.Data.Infrastructure.UnitOfWork'.


What I have tried:

Anyone can help me how to fix this? Thanks you!
Posted
Updated 7-Jul-18 23:38pm
v5

1 solution

I fixed that. I made a mistake when I create UnitOfWork file. constructor of it must be create public.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900