我正在使用 ASP.NET MVC 5。我是 AutoFac 的新手。我有很多课,每节课我都要做,
builder.RegisterType<AuthenticationService>().As<IAuthenticationService>();
builder.RegisterType<IAuthenticationRepositry>().As<AuthenticationRepositry>();
...............................................................................
...............................................................................
注册每种类型非常耗时,而且很容易忘记。有没有什么自动方法可以在 AutoFac 中注册组件?
最佳答案
如果你想自动注册所有类型作为它们的接口(interface),你可以使用RegisterAssemblyTypes
:
builder.RegisterAssemblyTypes(typeof(MvcApplication).Assembly).AsImplementedInterfaces();
或者AsSelf
,如果你想解析具体的实现。
关于c# - 手动注册所有类(class)还是有自动方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22603973/