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 7755875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:40:28+00:00 2026-06-01T12:40:28+00:00

I am having a problem resolving singleton components in Autofac and wondering if Autofac

  • 0

I am having a problem resolving singleton components in Autofac and wondering if Autofac is behaving correctly or if this is a genuine bug.

The way I understand Autofac is working is that when you register a singleton component, that registration gets “pulled” to the root container so that anything that resolves that component will resolve the same instance.

So, I am wondering if that same mechanism should come into play when registering the singleton component in separate child scopes. I’ll let the code speak for itself:

public class ChildSingletonScopeTest
{
    readonly IContainer container;
    readonly ILifetimeScope scope1, scope2, childScope1;

    public ChildSingletonScopeTest()
    {
        var builder = new ContainerBuilder();
        container = builder.Build();

        scope1 = container.BeginLifetimeScope(ConfigureTenant);
        scope2 = container.BeginLifetimeScope(ConfigureTenant);

        childScope1 = scope1.BeginLifetimeScope();
    }

    void ConfigureTenant(ContainerBuilder builder)
    {
        builder.RegisterType<Testing>().As<ITesting>().SingleInstance();
    }

    [Xunit.Fact] /* this test fails */
    public void should_resolve_single_instance_for_each_tenant_scope()
    {
        var instance1 = scope1.Resolve<ITesting>();
        var instance2 = scope2.Resolve<ITesting>();

        Xunit.Assert.Equal(instance1, instance2);
    }

    [Xunit.Fact] /* this test passes */
    public void should_resolve_single_instance_down_child_scope_hierarchy()
    {
        var instance1 = scope1.Resolve<ITesting>();
        var instance2 = childScope1.Resolve<ITesting>();

        Xunit.Assert.Equal(instance1, instance2);
    }
}

interface ITesting { }

class Testing : ITesting
{
    public override string ToString()
    {
        return GetHashCode().ToString();
    }
}

The reason that I am trying to do something crazy like this comes down to the way we are implementing multitenancy in our web app. Without getting too bogged down in the details, we have a requirement to be able to programmatically “switch” tenants in the middle of a web request.

The way we do this is by nesting the tenant scopes under each disposable request scope. However, tenant-specific singleton registrations are not resolving the same instance for the same tenant across different requests.

So, I am wondering if this is a bug in the way Autofac is handling these singleton registrations OR if I need to change the way I am dealing with singleton components in the tenant scopes.

  • 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-01T12:40:30+00:00Added an answer on June 1, 2026 at 12:40 pm

    From the Autofac wiki page on instance scopes…

    Using single instance scope, one instance is returned from all requests in the parent and all nested containers.

    Which means if you have nested scopes like this…

    container
      scope-1
        scope-2
    

    Then if you register a singleton in “scope-1” then when you resolve it in “scope-2” you’ll get the same singleton. On the other hand, if you try to resolve it in the container, you won’t get anything because it wasn’t registered there – the registrations “inherit” down the tree, not up, as your tests prove out.

    There is a nice primer on Autofac lifetime scopes and component resolution on Nick Blumhardt’s blog. One confusing thing you might see there is a mention of how singleton-scoped components get resolved if the registration isn’t in the current scope.

    Say you register a singleton in “scope-1” and that singleton has a dependency. Further, say the dependency is registered in both “scope-1” and “scope-2.” Now you resolve the singleton from “scope-2.” Since the singleton is registered in “scope-1” it will resolve its dependencies from “scope-1” instead of using your override in “scope-2” – because once “scope-2” ends, the singleton still has to stick around in “scope-1” for others to consume.

    In other areas of your scenario, you may also end up running into a “last-in-wins” thing, too. If you register a singleton in the “container” and then another in “scope-1,” when you resolve from “scope-2” you’ll get the one registered in “scope-1,” not the one from the root. However, you should also be able to resolve all of them, like…

    scope.Resolve<IEnumerable<IMySingleton>>();
    

    If you resolve an IEnumerable<T> of your component, you get all of the registered components, not just the last one you registered.

    I suppose that’s a long way of saying it’s not a bug, that’s how Autofac works.

    If you’re doing multitenancy with Autofac, have you looked at AutofacContrib.Multitenant? It’s one of the contributed libraries that has native support for things like tenant-based singletons. There is a wiki page on Autofac for that, too. You can configure your application-level defaults and then tenant-specific overrides. You can also do things like…

    containerBuilder.RegisterType<MyType>().As<IMyType>().InstancePerTenant();
    

    …which makes root-level registration of tenant singletons pretty simple.

    There isn’t a NuGet package for the contrib libraries in Autofac yet (we’re working on it) so you’ll have to go to the site and manually download, but it might help solve your problem.

    [Full disclosure: I wrote AutofacContrib.Multitenant.]

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

Sidebar

Related Questions

I am having an issue similar to this question: Problem with WCF and Multiple
Am having problems with PHP's require_once, require, include_once and include functions not correctly resolving
I'm having a problem with what the browser considers 'this' to be. In the
I'm having an issue with MSBuild 4.0 not resolving a path correctly in one
I'm working on some C++ type system stuff, and I'm having a problem removing
I'm having a problem resolving a hostname using python's (2.6.2) socket class. From the
Im having a problem with removing non-utf8 characters from string, which are not displaying
Having problem with the middle Div not expanding to the width http://acs.graphicsmayhem.com/images/middiv.jpg Ok, how
We're having problem with a huge number of legacy stored procedures at work. Do
I'm having problem in the following line: rd.PrintOptions.PaperSize = PaperSize.PaperFanfoldStdGerman; it throws an exception

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.