I would like to either change the service with a limited request, or install it at the user level of the middleware.
In particular, I want to be able to do something like the far-fetched example below in Startup.cs :
public void ConfigureServices(IServiceCollection service) { service.AddScoped<IMyUserDependentService>((provider) => { return new MyService()); }); } public void Configure(...) {
Then in the controller, do the following:
public class HomeController: Controller { public HomeController(IMyUserDependentService myService) {
The problem is that this does not work. myService.UserName is not Fred in the controller, this is null. I think the IOC container creates a new instance in the controller and does not use one set in the middleware.
If I changed the scope of the service to Transient, Fred will be remembered, but it will not help, because the service depends on who the current user is.
To repeat, I need to create / or edit a service that requires the current user (or other current request variables), but I cannot handle this.
Thanks in advance!
source share