Dependency injection in ASP.NET

Posted by Joe Enos on May 30, 2021 · 1 min read

Out of the box, you get dependency injection in ASP.NET, and can inject items into a controller constructor, or a view, with a very simple configuration step. IConfiguration comes free, as does logging. And the framework provides others, like IHttpContextAccessor, which lets you easily access HTTP stuff outside of a controller or view.

You get scoped, transient, and singleton items, and automatic disposal for scoped items once a request is completed.

Learn more about dependency injection in the docs.

View code on GitHub