Custom Middleware in ASP.NET

Option #1: Implementing IMiddleware Add the middleware to DI, and then use it. In the middleware class, use DI for the constructor, and implement the single InvokeAsync method. Option #2: By Convention Use the middleware, but you don’t need to register it in DI. In the middleware class constructor, accept and store the RequestDelegate, then […]

Simple Dependency Injection in .NET Core Console App

If you’re using ASP.NET Core, you get dependency injection out of the box. If you’re building a console app, you can get simple dependency injection with just a little bit of code. For basic mapping, all you need is to create a ServiceProvider object that maps  your types, and your constructor injection works perfectly. Here’s […]

Dependency injection in ASP.NET

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 […]