Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I add a new scoped service within a custom middleware?

I'm using WebApi in Asp .Net Core and I'm wondering if/how I can add a new scoped service that all following middleware and controllers can get access to through dependency injection? Or should I share state with HttpContext.Items instead? That doesn't seem to be what it is intended for since HttpContext isn't available at all in a WebApi-controller?

If neither HttpContext or DI is the right tool for this, then how can I propagate state in the request-pipeline without having it already created from the beginning?

like image 668
Andreas Zita Avatar asked Mar 23 '17 18:03

Andreas Zita


People also ask

Should service be scoped or transient?

Use Transient lifetime for the lightweight service with little or no state. Scoped services service is the better option when you want to maintain state within a request. Singletons are created only once and not destroyed until the end of the Application. Any memory leaks in these services will build up over time.

What is scoped service?

In a scoped service, with every HTTP request, we get a new instance. However, within the same HTTP request, if the service is required in multiple places, like in the view and in the controller, then the same instance is provided for the entire scope of that HTTP request.

Can we create custom Middlewares?

We can add middleware using app. UseMiddleware<MyMiddleware>() method of IApplicationBuilder also. Thus, we can add custom middleware in the ASP.NET Core application.

How do I share a scoped service between two middleware types?

Because middleware is constructed at app startup, not per-request, scoped lifetime services used by middleware constructors aren't shared with other dependency-injected types during each request. If you must share a scoped service between your middleware and other types, add these services to the InvokeAsync method's signature.

Can we add custom middleware in the ASP NET Core Application?

Thus, we can add custom middleware in the ASP.NET Core application. Want to check how much you know ASP.NET Core?

How to add custom middleware in Salesforce?

Add Custom Middleware. Select Middleware Class item and give it a name and click on Add button. This will add a new class for the middleware with extension method as shown below. Example: Custom Middleware.

How do I create a scoped service within a backgroundservice?

To use scoped services within a BackgroundService, create a scope. No scope is created for a hosted service by default. The scoped background service contains the background task's logic. The preceding interface defines a single DoWorkAsync method. To define the default implementation: The service is asynchronous.


1 Answers

First add your scoped service in your ConfigureServices(IServiceCollection services)

services.AddScoped<IMyService, MyService>();

Then, the only way I know of to get a scoped service injected into your middleware is to inject it into the Invoke method of your Middlware

public class MyMiddleware
{
    private readonly RequestDelegate _next;

    public MyMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext httpContext, IMyService service)
    {
        service.DoSomething();
        await _next(httpContext);
    }
}

Injecting in the constructor of MyMiddleware will make it a singleton by default as it's only called on startup. Invoke is called every time and dependency injection will grab the scoped object.

like image 132
Mike H Avatar answered Oct 11 '22 11:10

Mike H