Autofac Singleton and non singleton together

That is my AutoFac setup:

  builder.RegisterType<CachingOuterClass>().SingleInstance(); // Singleton
  builder.RegisterType<InnerClassAzureClient>().InstancePerRequest();

How can this setup work?

public class CachingOuterClass
{
   public CachingOuterClass(InnerClassAzureClient innerClassAzureClient)
  {
      //...
  }
}

When the caching outer class is saved forever and once instantiated how can the azure client class be passed by api request?

Answers


There's a detailed explanation of this issue in the FAQ. It's too long to put here as an answer.

It does cover your specific question:

As you analyze the registrations in the problem dependency chain, look at the lifetime scopes for which they’re registered. If you have a component registered as SingleInstance() but it (maybe indirectly) consumes a component registered as InstancePerRequest(), that’s a problem. The SingleInstance() component will grab its dependencies when it’s resolved the first time and never let go. If that happens at app startup or in a background thread where there’s no current request, you’ll see this exception. You may need to adjust some component lifetime scopes. Again, it’s really good to know how dependency lifetime scopes work in general.


Need Your Help

How to fetch an URL containing non-ASCII characters (ą, ś ...) with Jsoup?

java jsoup

I am using jsoup to parse some polish sites, but I have problem with special characters like "ą", "ś" in URL(!), for example example.com/kąt is readed like example.com/k

Php + MySQL banner rotator by order

php mysql banner

I have table with advertisement in MySQL. I would like to rotate banners by order (NOT RANDOM). What function or mechanism I need to SELECT advertisement from MySQL table to show it in order, like 1,