Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ThreadScopedLifestyle vs AsyncScopedLifestyle #991

Open
vkirienko opened this issue Feb 13, 2024 · 1 comment
Open

ThreadScopedLifestyle vs AsyncScopedLifestyle #991

vkirienko opened this issue Feb 13, 2024 · 1 comment
Labels

Comments

@vkirienko
Copy link

vkirienko commented Feb 13, 2024

HI,

I'm trying to understand what is real life difference between ThreadScopedLifestyle and AsyncScopedLifestyle in .NET Core application.

We have services that are used during HTTP request processing as well as in background jobs on explicitly created threads with ThreadScopedLifestyle scope. For that we created HybridLifestyle that uses one or another based on presence of HttpContext, that supports MVC, WebApi and background jobs.

Now comes migration to .NET Core and I wonder if really need HybridLifestyle like below at all or simple AsyncScopedLifestyle would be good to cover all three cases.

        private static readonly ScopedLifestyle HybridLifestyle =
            Lifestyle.CreateHybrid(
                defaultLifestyle: new AsyncScopedLifestyle(),
                fallbackLifestyle: new ThreadScopedLifestyle()
            );

Thank you.

@dotnetjunkie
Copy link
Collaborator

dotnetjunkie commented Feb 14, 2024

The ThreadScopedLifestyle is a legacy lifestyle, which is included solely for backwards compatibility. For modern applications, you should primarily use the AsyncScopedLifestyle as this lifestyle works in both async and single-threaded scenarios.

When solely depending on the AsyncScopedLifestyle as scoped lifestyle, there is also no need to use the hybrid lifestyle.

This simplifies your registration as you can simply do the following:

var container = new Container();
container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();

container.Register<IUnitOfWork, SqlUnitOfWork>(Lifestyle.Scoped);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants