My Models和DataContext是我解决方案中我的Web MVC项目的另一个项目。当我尝试在AppHost
Configure
方法中注册我的DataContext时,当我尝试在我的服务将使用的存储库中使用它时,我的DataContext仍为null
。
这是Configure
:
container.Register<WarrantyContext>(c => new WarrantyContext());
然后,当我尝试在存储库中使用依赖项时,如下所示:
public WarrantyContext _db { get; set; }
它仍然是null
。尝试从另一个项目注册依赖项时,您有什么需要做的吗?
答案 0 :(得分:1)
有些事情需要研究......
如果您使用的是autofac,请确保您已按照列出的所有步骤here
听起来你有一个Repository类,它将WarrantyContext作为属性。有一个注释here表示“当使用上述方法时,注册类型的属性和构造函数不会自动连接(即不注入属性和构造函数)。需要像“那样手动完成。因此,如果您将一个类Repository(具有WarrantyContext属性)注入您的服务(如下所示),WarrantyContext将为null,除非您手动注册它。
//To inject WarrantyContext into a Repository that is injected to Service
container.Register<Repository>(c => new Repository() { _db = c.Resolve<WarrantyContext>() });
public class Repository
{
public WarrantyContext _db { get; set; }
}
public class AService : Service
{
public Repository repo { get; set; }
public object Any(ARequest request)
{
//CODE
}
}