0

I'm looking for information on how to dynamically inject services into a class method using Dependency injection in ASP.Net Core.

For example, in ASP Middleware, you can inject any number of services into the the Invoke method, as opposed to the constructor. This documentation explains how you can inject scoped services into your Middeware's Invoke method:

public class CustomMiddleware { private readonly RequestDelegate _next; public CustomMiddleware(RequestDelegate next) { _next = next; } // IMyScopedService is injected into Invoke public async Task Invoke(HttpContext httpContext, IMyScopedService svc) { svc.MyProperty = 1000; await _next(httpContext); } } 

I would like to mimic the same behavior in my own code, but I cannot find any documentation explain how to achieve this. Currently I am creating a scope using IServiceProvider.CreateScope and passing the resulting IServiceProvider as a parameter to my function defined below:

public async Task DoWork(IServiceProvider services, CancellationToken cancellationToken) { var logger = services.GetRequiredService<ILogger<RoomController>>(); logger.LogInformation("Starting Room Controller."); while (!cancellationToken.IsCancellationRequested) { logger.LogInformation("Room still running"); Thread.Sleep(5000); } } 

I would like my function to look more like this:

public async Task DoWork(CancellationToken cancellationToken, ILogger<RoomController> logger) { logger.LogInformation("Starting Room Controller."); while (!cancellationToken.IsCancellationRequested) { logger.LogInformation("Room still running"); Thread.Sleep(5000); } } 

Any ideas?

2 Answers 2

2

I was also curious so I went digging into github to see how it's done for middleware. Turns out it's largely a manual process:

https://github.com/aspnet/AspNetCore/blob/425c196cba530b161b120a57af8f1dd513b96f67/src/Http/Http.Abstractions/src/Extensions/UseMiddlewareExtensions.cs

(Not copying the code here as it's quite a lot)

They inspect the method signature and compile a factory delegate which retrieves the required services from the IServiceProvider which lives in the HttpContext.

Sign up to request clarification or add additional context in comments.

Comments

0

Hope Dependency injection in ASP.NET Core can help :

  • Registration of the dependency in a service container. ASP.NET Core provides a built-in service container, IServiceProvider. Services are registered in the app's Startup.ConfigureServices method.

You need to register in ConfigureService how you dependency injection should be resolved.

2 Comments

I am aware of how dependency injection works, but there is no documentation on that page describing how to achieve what I wrote in the original post.
Dies it help to add in ‘ConfugureService’ something like ‘services.AddTransient<ILogger<RoomController>, Logger<RoomController>>()’ and do ‘Invoke’ as you wish to do?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.