When my application starts, I have a bunch of modules (module1, module2 ...). For each of this module, I have a bunch of controller actions:
/myModuleController/module1/action1 /myModuleController/module1/action2 /myModuleController/module2/action1 /myModuleController/module2/action2 โฆ
Since the user can register himself once for each module, I deploy middleware for authentication on one module, which runs as follows:
app.UseWhen((context) => context.Request.Path.StartsWithSegments(urlPath), appbuilder => { appbuilder.UseCookieAuthentication(new CookieAuthenticationOptions() { CookieName = cookieName, โฆ }); });
So basically, in the path of URL /myModuleController/module1 , I have one middleware plus its cookie, the other for /myModuleController/module2 ... Its a bit unusual, but I think, but its work is fine, I am pleased with the behavior.
Here's the problem: I want to be able to add a new module at run time, which would imply the ability to deploy new middleware with a piece of code, for example app.UseWhen(url, app. UseCookieAuthentication(โฆ)) . I tried naively to inject the IApplicationBuilder app into the controller responsible for adding the module, but Im getting an exception:
System.InvalidOperationException: Cannot resolve service for type "Microsoft.AspNetCore.Builder.IApplicationBuilder" when trying to activate "AdminController"
My question is for you: should it work, and should I make a mistake somewhere? or, is it clear to you that what I'm trying here has no chance of work?
How would you achieve the same requirement? Thanks.