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.

Enable DI container scope validation by default for Development environment in all ASP.NET Core 2.0 templates

See original GitHub issue

Update all the ASP.NET Core 2.0 templates to turn on scope validation when in the development environment, e.g.:

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        HostingEnvironment = env;
    }

    public IHostingEnvironment HostingEnvironment { get; }

    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        // Set up container here...
        
        return services.BuildServiceProvider(validateScopes: HostingEnvironment.IsDevelopment());
    }

    public void Configure()
    {
        ...
    }
}

@glennc @davidfowl @pakrym @mlorbetske

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
davidfowlcommented, May 21, 2019

Scope validation makes sure your dependency graph doesn’t have mismatched lifetimes (like depending on a scoped service from a singleton service). Read about “Captive dependencies” https://blog.ploeh.dk/2014/06/02/captive-dependency/

Simple injector also has a feature to detect this - https://simpleinjector.readthedocs.io/en/latest/LifestyleMismatches.html

2reactions
davidfowlcommented, Sep 5, 2017

We didn’t want the perf hit in production. Since you rarely change registrations at runtime (if ever) you’d catch most things at development time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I validate the DI container in ASP.NET Core?
A built-in DI container validation was added in ASP.NET Core 3 and it is enabled only in the Development environment by default.
Read more >
Dependency injection in ASP.NET Core
The framework creates a scope per request, and RequestServices exposes the scoped service provider. All scoped services are valid for as long as ......
Read more >
New in ASP.NET Core 3: Service provider validation
In this post I describe the new "validate on build" feature that has been added to ASP.NET Core 3.0. This can be used...
Read more >
Filters in ASP.NET Core
Learn how filters work and how to use them in ASP.NET Core. ... How filters work; Implementation; Filter scopes and order of execution ......
Read more >
ASP.NET Core Web API Best Practices
In this post, we are going to write about what we consider to be the best practices while developing the .NET Core Web...
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