There are several ways of how you can do that:
The best one in my opinion. You can register the repository as InstancePerLifetimeScope as you said. It works with HttpRequests and LifetimeScopes equally well.
builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>)) .InstancePerLifetimeScope();
Your registration for HttpRequest may differ from registration for LifetimeScope, then you can have two separate registrations:
builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>)) .WithParameter(...) .InstancePerHttpRequest(); // will be resolved per HttpRequest builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>)) .InstancePerLifetimeScope(); // will be resolved per LifetimeScope
You can explicitly create
"HttpRequest"
scope using its tag. Exposed throughMatchingScopeLifetimeTags.RequestLifetimeScopeTag
property in new versions.using (var httpRequestScope = container.BeginLifetimeScope("httpRequest")) // or "AutofacWebRequest" for MVC4/5 integrations { var repository = httpRequestScope.Resolve<IRepository<Entity>>(); }