-
Notifications
You must be signed in to change notification settings - Fork 152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implementing IMiddlewareFactory with Simple Injector for ASP.NET Core #510
Comments
Are you calling Apart from that, is there any particular reason to use this specific method of applying middleware (going through the framework provided factory abstraction) instead of using the approach described in the docs? |
See [@]davidfowl's comment 👉 aspnet/HttpAbstractions#754 (comment)
Yes, thanks, that seems to have helped. I saw it in the docs, but there's no explicit explanation of what it does. I thought it might just be part of controller or view component activation. This project still doesn't quite work, but it's getting closer ... Now, the problem is that the Razor Page is choking with "can't find the Registering the page class, I have been able to confirm that the middleware is working and adding entries for each request. I think the last problem is getting the page to use that dB context. It all should work after that. |
Update Wiring up for
I certainly can convert this app over to MVC Core. Let me know if there's some other way to get SI to take over the DI for a Razor Page. I'd prefer to keep this a Razor Pages sample if possible. |
Prevent injecting any dependencies into razor pages in the first place. See this discussion for more details:
The |
Ah! I see. Thanks. As an aside, I hope [@]DamianEdwards received that feedback. Anywho ... the workaround in #362 makes perfect sense. Thanks again! This sample is almost there! |
🎉 🎈 🍻 Booyeah! Thank you so much for those tips. That permits me to shoot this updated sample back into the PR. I was meeting some requests from [@]davidfowl to make sure that SI was doing all of the DI work and handling the factory activation to execute the middleware. I'll ping you over on the docs PR shortly, and I hope you'll take a look and provide feedback. Thanks again for your help! |
I would advise against using Simple Injector as example for demonstrating For Simple Injector, a much simpler model is to have the following extension method: public static class SimpleInjectorMiddlewareExtensions
{
public static void UseMiddleware<TMiddleware>(
this IApplicationBuilder app, Container container)
where TMiddleware : IMiddleware
{
container.Register<TMiddleware>(Lifestyle.Transient);
app.Use((c, next) => container.GetInstance<TMiddleware>().InvokeAsync(c, _ => next()));
}
} This extension method can be used as follows to add middleware to the pipeline: public void Configure(
IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseMiddleware<MyCustomMiddleware>(this.container);
} We might even add this extension method to the ASP.NET Core integration package. |
@dotnetjunkie Why do you think that approach is cleaner? Is it because the IMiddlewareFactory is too global? |
@davidfowl explain what the problem is that the factory solves. |
Today the issue with conventional middleware is that it takes arguments that aren't services. The In order to fix this, we added |
I wholeheartedly agree that this is a problem. The underlying problem here is that the current model forces the injection of runtime data (the So it's great to hear you are addressing this, and the the The The addition of the The documentation example for |
No problem! |
@dotnetjunkie As of this moment, I'm just following orders. I DO stand ready to create a new sample using another container if they order such a move. In the meantime, I hope I reduce your concerns a tad with some very strong language in the note that will be added to the topic on dotnet/AspNetCore.Docs#5382. |
Hi Steven (@dotnetjunkie),
I'm working on the advanced
IMiddleware
/IMiddlewareFactory
topic for the ASP.NET Core docs, and I've hit a snag with scope lifetimes that I can't work out. I probably have created a completely bonkers wiring-up job and/or factory! lolThe introductory topic that compares conventional middleware activation with factory-based activation is here 👉 https://docs.microsoft.com/aspnet/core/fundamentals/middleware/extensibility
I'm working on a new topic that uses the factory with Simple Injector. Everything seemed to be going along fairly well until I hit ye 'olde scoping problems, such as ...
The middleware is unremarkable ... just writes a query string value to an in-memory dB:
I'd like the factory to get that going. I can't confirm this works because of the scoping issue, but here's what I was thinking ...
... an unremarkable pipeline for this sample (trying to keep the
Startup
class lean, so I movedInitializeContainer
bits up toConfigureServices
) ...... and the wiring up bits in
ConfigureServices
look like this (again, dropping theIntegrateSimpleInjector
format to keep things lean) ...The full project is over here 👉 https://github.com/guardrex/MiddlewareExtensibilitySample2
... and the scopes you see in the GH project and here ☝️ I hacked around with trying to resolve this but no luck. It actually seems like lifestyles aren't my problem and that I haven't wired Simple Injector up properly or I built a broken factory.
The text was updated successfully, but these errors were encountered: