question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DI Lifetime.Request works per thread.

See original GitHub issue

Brief explanation of issue on forum: https://our.umbraco.com/forum/umbraco-8//98389-iusercomposer-with-lifetimerequest-works-as-lifetimetransient

In general: All would have been ok with DI, if i didnt need custom EF db context, not related to umbraco content - my own db for a lot of businesslogic data structure, for webapi and custom mvc.

So issue in general: EntityContext.Instance() gets fired 35 times during request. I did static counter, and it is seen that it was created 35 times during request. Why Lifetime.Request doesnt create 1 instance per request? (all server side use async awaited Tasks)

Apart from that Lifetime.Request dependencies gets disposed at the end of first thread, so in controller constructor i have ok context, but in controller method it is already disposed. Also every single dependency on EntityContext has its own instance, instead 1 instance per request. So my db context and transactions are disposed before being injected. And all dependencies have different db contexts - checked by static counter with instance saved value.

Entire concept of UnitOfWork with services breaks, as i cant handle all db updates at once which is important for business logic.

I tried to create own Container and resolve DI with DependencyResolver, but entire backoffice stop to work - i can’t even login (so umbraco uses DependencyResolver), also umbraco dependent instances wasnt loaded.

Is it a known issue? i had 8.0.2 version, and then updated to 8.1 - issue still exist. The only specific thing is that i extended UmbracoDefaultOwinStartup, as it was needed for webapi. rest shouldn’t do anything to default installation.

public partial class Startup : UmbracoDefaultOwinStartup
    {
        public override void Configuration(IAppBuilder app)
        {
            base.Configuration(app);

            ServicesConfig.RegisterServices(app);          
            FluentValidationModelValidatorProvider.Configure(x => x.AddImplicitRequiredValidator = true);
            AuthConfiguration.ConfigureAuth(app);
        }

        protected override void ConfigureServices(IAppBuilder app, ServiceContext services)
        {
            base.ConfigureServices(app, services);
            ServicesConfig.RegisterServices(app, services);            
        }

        protected override void ConfigureMiddleware(IAppBuilder app)
        {
            base.ConfigureMiddleware(app);            
        }        
    }

Would be cool to get some answer or suggestions on forum 😃

P.S. 1 little update - just have tried after post. changed back owin startup to UmbracoDefaultOwinStartup. And now all instances being disposed after controllers method execution. Tada! (so extending UmbracoDefaultOwinStartup causes DI issues?) but still instead of 1 instance per request - many instances.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:22 (15 by maintainers)

github_iconTop GitHub Comments

3reactions
Shazwazzacommented, Aug 7, 2019

@kjac Yep I agree. What we can do is obsolete and hide Lifetime.Scope since internally we don’t use it (well we actually do, but that’s because we actually wanted ‘one per request’ in some cases). In some cases people would want to use the container’s own Scope based lifetime but in those cases, developers can do this directly using the container by accessing the IRegister.Concrete property to return the concrete underlying container. I can submit a PR shortly for 8.2.

In the meantime, anyone wanting to have a lifetime of “one per request” must use Lifetime.Scope

1reaction
Shazwazzacommented, Aug 8, 2019

Have created a PR here https://github.com/umbraco/Umbraco-CMS/pull/6069

Have decided to not obsolete Lifetime.Scope since there will be use cases for this for some people and since this is a native feature for every container.

Read more comments on GitHub >

github_iconTop Results From Across the Web

When to use PerThreadLifetimeManager?
The Per Thread Lifetime is a very dangerous lifestyle and in general you should not use it in your application, especially web applications....
Read more >
iusercomposer with lifetimerequest works as lifetimetransient
I am using umbraco containers for DI, and it works pretty well exapt the thing that Lifetime.Request is not working.
Read more >
NET Core Dependency Injection Lifetimes Explained
In web terms, it means that after the initial request of the service, every subsequent request will use the same instance. This also...
Read more >
How do I work with per-request lifetime scope?
A very common exception people see when they start working with per-request lifetime scope is: No scope with a tag matching 'AutofacWebRequest' is ......
Read more >
Danger! Dependency Injection Scoped Lifetime and Thread ...
If you're using Scoped Lifetime in a Dependency Injection Container, it's possible to run into race conditions. Here's a simple demo of how....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found