将服务注入DbContext不起作用

时间:2017-01-10 19:21:36

标签: c# asp.net entity-framework dependency-injection dbcontext

我收到Microsoft.Practices.Unity.dll

中发生的'System.StackOverflowException'类型的未处理异常
public SomeDbContext(IAuditLogService service)
            : base("name=SomeDb")
{

}

public static SomeDbContext Create()
{
   using (var container = new UnityContainer())
   {
      var service = container.Resolve<IAuditLogService>();
      return new SomeDbContext(service);
   }
}

container.RegisterType<DbContext, SomeDbContext>(new TransientLifetimeManager());
container.RegisterType<ISomeDbContext, SomeDbContext>(new TransientLifetimeManager());

container.RegisterType<IAuditLogService, AuditLogService>(new HierarchicalLifetimeManager());
container.RegisterType<IAuditLogManager, AuditLogManager>(new HierarchicalLifetimeManager());

AuditLogService具有以下构造函数:

public AuditLogService(IAuditLogManager auditLogManager)
{
}

和AuditLogManager有:

public AuditLogManager(ISomeDbContext context) : base(context)
{
}

基本上我想在调用SaveChanges()时插入一些日志。我想用DI传递AuditLogService。

我该如何设置?

0 个答案:

没有答案