2

I have an asp.net MVC site which has many components registered using an InstancePerHttpRequest scope, however I also have a "background task" which will run every few hours which will not have an httpcontext.

I would like to get an instance of my IRepository which has been registered like this

builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>))
     .InstancePerHttpRequest();

How do I do this from a non http context using Autofac? I think the IRepository should use the InstancePerLifetimeScope

Paul
  • 1,457
  • 1
  • 19
  • 36

1 Answers1

6

There are several ways of how you can do that:

  1. 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();
    
  2. 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
    
  3. You can explicitly create "HttpRequest" scope using its tag. Exposed through MatchingScopeLifetimeTags.RequestLifetimeScopeTag property in new versions.

    using (var httpRequestScope = container.BeginLifetimeScope("httpRequest")) // or "AutofacWebRequest" for MVC4/5 integrations
    {
        var repository = httpRequestScope.Resolve<IRepository<Entity>>();
    }
    
Alexandr Nikitin
  • 7,258
  • 2
  • 34
  • 42
  • I did option 3 like this: _container.BeginLifetimeScope("AutofacWebRequest") – Paul Aug 04 '14 at 01:01
  • @Paul Thanks. Request tag was changed to "AutofacWebRequest" and exposed through MatchingScopeLifetimeTags.RequestLifetimeScopeTag in new versions. Updated the answer. – Alexandr Nikitin Aug 04 '14 at 06:15
  • #1 works well. To get container you can do the following: `var container = AutofacDependencyResolver.Current.ApplicationContainer;` – Sam Sippe Sep 23 '14 at 04:07