我在SO上已经阅读了很多关于这个问题的问题,但我无法弄清楚我的实现有什么问题。
我遇到以下异常:
没有任何构造函数与' Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type' SampleAuthTemplate.Domain.Entities.Core.Repositories.Concrete.EntityRepository`1 [SampleAuthTemplate.Domain.Entities.Order]'可以使用可用的服务和参数调用: ↵无法解析参数' SampleAuthTemplate.Domain.Entities.Core.EntitiesContext entitiesContext'构造函数' Void .ctor(SampleAuthTemplate.Domain.Entities.Core.EntitiesContext)'。"
以下是AutoFac注册:
private static IContainer RegisterServices(ContainerBuilder builder)
{
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
// registration goes here
builder.RegisterType<EntitiesContext>().As<IdentityDbContext<ApplicationUser>>().InstancePerApiRequest();
builder.RegisterGeneric(typeof(EntityRepository<>)).As(typeof(IEntityRepository<>)).InstancePerApiRequest();
//builder.RegisterGeneric(typeof(AuthRepository<>)).As(typeof(IAuthRepository<>)).InstancePerApiRequest();
builder.RegisterType<AuthRepository>().As<IAuthRepository>().InstancePerApiRequest();
builder.RegisterType<OrderService>().As<IOrderService>().InstancePerApiRequest();
return builder.Build();
}
和EntitiesContext类:
public class EntitiesContext : IdentityDbContext<ApplicationUser>
{
public EntitiesContext(): base("SampleApp")
{
}
public IDbSet<Order> Orders {get;set;}
public IDbSet<OrderDetail> OrderDetails { get; set; }
}
请指导我。
谢谢。
答案 0 :(得分:4)
注射网站需要EntitiesContext
,但您只需为IdentityDbContext<ApplicationUser>
注册。
试试这个:
builder.RegisterType<EntitiesContext>().AsSelf()
.As<IdentityDbContext<ApplicationUser>>().InstancePerApiRequest();