我正在构建一个Azure移动服务(WebApi应用程序),互联网告诉我,我需要使用一种形式的DI来管理像我的DbContext这样的东西。但是,如果我将DbContext标记为
,我会遇到一些问题 .InstancePerRequest()
的错误
从请求实例的作用域中看不到具有匹配“AutofacWebRequest”的标记的作用域。这通常表示SingleInstance()组件(或类似场景)正在请求注册为每HTTP请求的组件。在Web集成下,始终从DependencyResolver.Current或ILifetimeScopeProvider.RequestLifetime请求依赖项,从不从容器本身请求。
InstancePerLifetimeScope()
的错误
已经有一个与此命令关联的打开DataReader,必须先关闭它。
我已经尝试了.InstancePer-blah-blah的每一个组合我认为是有道理的,到目前为止,似乎没有任何工作正常。
现在,我注意到的一件事是,在Azure Mobile Service模板中,在控制器初始化方法中创建了一个上下文并传递给域管理器,如下所示:
protected override void Initialize(HttpControllerContext controllerContext)
{
base.Initialize(controllerContext);
MobileServiceContext context = new MobileServiceContext();
DomainManager = new EntityDomainManager<TodoItem>(context, Request, Services);
}
如果我无法弄清楚如何使autofac正常工作,如果将此应用程序用作我的移动应用程序的后端,那么在初始化方法中实例化具体类是不是太大了?
这是我的自动转码:
var builder = new ConfigBuilder(options, (httpConfig, autofac) =>
{
autofac.RegisterType<JapaneseHubDataModel>()
.AsSelf()
.InstancePerLifetimeScope(); // <-- the poorly named DbContext
// register the different repositories
autofac.RegisterGeneric(typeof(JapaneseHubRepository<>)) // an extension of the prior dbContext
.As(typeof(IJapaneseHubRepository<>))
.UsingConstructor(typeof(JapaneseHubDataModel))
.InstancePerLifetimeScope();
autofac.RegisterGeneric(typeof(UserReferenceRepository<>)) // an extension of JapaneseHubRepository
.As(typeof(IUserReferenceRepository<>))
.UsingConstructor(typeof(JapaneseHubDataModel))
.InstancePerLifetimeScope();
autofac.RegisterType(typeof(UserRepository))
.As(typeof (IUserRepository))
.InstancePerLifetimeScope();
// TODO: figure a way to do this the DRY way
// set the targetted constructors
autofac.RegisterType<AppSettingController>()
.UsingConstructor(typeof(IUserReferenceRepository<AppSetting>), typeof(ApiServices));
autofac.RegisterType<ProgressController>()
.UsingConstructor(typeof(IUserReferenceRepository<Progress>), typeof(ApiServices));
autofac.RegisterType<UsageEntryController>()
.UsingConstructor(typeof(IUserReferenceRepository<UsageEntry>), typeof(ApiServices));
autofac.RegisterType<ReviewItemController>()
.UsingConstructor(typeof(IUserReferenceRepository<ReviewItem>), typeof(ApiServices));
autofac.RegisterType<PurchaseItemController>()
.UsingConstructor(typeof(IUserReferenceRepository<PurchaseItem>), typeof(ApiServices));
autofac.RegisterType<UserController>()
.UsingConstructor(typeof (IUserRepository), typeof (ApiServices));
// register the controllers with their constructors.
autofac.Register(c => new AppSettingController(c.Resolve<IUserReferenceRepository<AppSetting>>(),
c.Resolve<ApiServices>()));
autofac.Register(c => new ProgressController(c.Resolve<IUserReferenceRepository<Progress>>(),
c.Resolve<ApiServices>()));
autofac.Register(c => new UsageEntryController(c.Resolve<IUserReferenceRepository<UsageEntry>>(),
c.Resolve<ApiServices>()));
autofac.Register(c => new ReviewItemController(c.Resolve<IUserReferenceRepository<ReviewItem>>(),
c.Resolve<ApiServices>()));
autofac.Register(c => new PurchaseItemController(c.Resolve<IUserReferenceRepository<PurchaseItem>>(),
c.Resolve<ApiServices>()));
autofac.Register(c => new UserController(c.Resolve<IUserRepository>(),
c.Resolve<ApiServices>()));
});
控制器:
public class AppSettingController : UserReferenceController<AppSetting>
{
public AppSettingController(IUserReferenceRepository<AppSetting> userRepo, ApiServices services)
: base(userRepo, services)
{ }
}