Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8030517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:47:43+00:00 2026-06-05T00:47:43+00:00

Problem (abstract) Given a module which registers dependency X. The dependency X has a

  • 0

Problem (abstract)

Given a module which registers dependency X. The dependency X has a different lifetime in a MVC3 app (lifetime per HttpRequest) then in a console application (dependency per lifetimescope with a name). Where or how to specify the lifetime of dependency X?

Case

I’ve put all my database related code in a assembly with a module in it which registers all repositories. Now the ISession (Nhibernate) registration is also in the module.

ISession is dependency X (in the given problem case). ISession has different lifetime in a MVC3 app (lifetime per request) then in a console app where I define a named lifetimescope.

Should the registration of ISession be outside the module? Would be strange since it’s an implementation detail.

What is the best case to do here? Design flaw or are there smart constructions for this 🙂 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T00:47:45+00:00Added an answer on June 5, 2026 at 12:47 am

    Given your use case description, I’d say you have a few of options.

    First, you could just have each application register their own set of dependencies including lifetime scope. Having one or two “duplicate” pieces of code in this respect isn’t that big of a deal considering the differences between the application and the fact that the registrations appear fairly small.

    Second, you could wrap the common part (minus lifetime scope) into a ContainerBuilder extension method that could be used in each application. It would still mean each app has a little “duplicate code” but the common logic would be wrapped in a simple extension.

    public static IRegistrationBuilder<TLimit, ScanningActivatorData, DynamicRegistrationStyle>
      RegisterConnection<TLimit, ScanningActivatorData, DynamicRegistrationStyle>(this ContainerBuilder builder)
    {
      // Put the common logic here:
      builder.Register(...).AsImplementedInterfaces();
    }
    

    Consuming such an extension in each app would look like:

    builder.RegisterConnection().InstancePerHttpRequest();
    // or
    builder.RegisterConnection().InstancePerLifetimeScope();
    

    Finally, if you know it’s either web or non-web, you could make a custom module that handles the switch:

    public class ConnectionModule : Autofac.Module
    {
      bool _isWeb;
      public ConnectionModule(bool isWeb)
      {
        this._isWeb = isWeb;
      }
    
      protected override void Load(ContainerBuilder builder)
      {
        var reg = builder.Register(...).AsImplementedInterfaces();
        if(this._isWeb)
        {
          reg.InstancePerHttpRequest();
        }
        else
        {
          reg.InstancePerLifetimeScope();
        }
      }
    }
    

    In each application, you could then register the module:

    // Web application:
    builder.RegisterModule(new ConnectionModule(true));
    
    // Non-web application:
    builder.RegisterModule(new ConnectionModule(false));
    

    Alternatively, you mentioned your lifetime scope in your other apps has a name. You could make your module take the name:

    public class ConnectionModule : Autofac.Module
    {
      object _scopeTag;
      public ConnectionModule(object scopeTag)
      {
        this._scopeTag = scopeTag;
      }
    
      protected override void Load(ContainerBuilder builder)
      {
        var reg = builder.Register(...)
                         .AsImplementedInterfaces()
                         .InstancePerMatchingLifetimeScope(this._scopeTag);
      }
    }
    

    Consumption is similar:

    // Web application (using the standard tag normally provided):
    builder.RegisterModule(new ConnectionModule("httpRequest"));
    
    // Non-web application (using your custom scope name):
    builder.RegisterModule(new ConnectionModule("yourOtherScopeName"));
    

    I would recommend against simply using InstancePerLifetimeScope in a web application unless that’s actually what you intend. As noted in other answers/comments, InstancePerHttpRequest uses a specific named lifetime scope so that it’s safe to create child lifetime scopes; using InstancePerLifetimeScope doesn’t have such a restriction so you’ll actually get one connection per child scope rather than one connection for the request. I, personally, don’t assume that other developers won’t make use of child lifetime scopes (which is a recommended practice), so in my applications I’m very specific. If you’re in total control of your application and you can assure that you aren’t creating additional child scopes or that you actually do want one connection per scope, then maybe InstancePerLifetimeScope will solve your problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: to show that a Not-SO-page has been discussed in SO when you at
(Problem solved now, see answers below) Hello, in my app I am trying to
Bit of an abstract problem here. I'm experimenting with the Domain Model pattern, and
Given the following code : public abstract class Participant { private String fullName; public
I have a data object which has a base class with three derived classes,
I have a problem with these two classes: Creature: public abstract class Creature extends
I have a base class which contains an abstract/MustInherit method. I want this method
I have an abstract parent class which child classes that inherit from it. I
Problem: I am having trouble implementing a recursive image lazy load in all relevant
problem I'm using LilyPond to typeset sheet music for a church choir to perform.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.