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
Suggestion/Question: Making automatic wireup of ASP.NET Core or other CancellationToken easier #496
Comments
This looks like a pretty elegant solution to me. The only thing I would change with this would be to inject the original mediator from CI and not build up your own internally. |
That is actually a good idea, but injecting the original mediator is not that easy without some extra work I think. This will result in a stackoverflow:
This will not work because the Mediator is only bound via the IMediator interface, not its implementation:
Any suggestions? |
Can you bind it via implementation just before?
not ideal but might get you there. Other DI like structuremap have methods for decorating but looks like aspnetcore doesn't have any natively other than the above |
That seems to work, yea. Is this solution something that would make sense in an extension library you provide? Outfitted with an extension method on the MediatR setup of ASP.NET Core named UseHttpRequestCancellationToken() (or similar) this could probably make a lot of people's lives easier. I could refine it a bit and create a PR if you want. Otherwise I will just close this issue and just leave it for documentation purposes in case anyone else wants to solve the same problem. |
I could see it being part of https://github.com/jbogard/MediatR.Extensions.Microsoft.DependencyInjection as a build in custom mediator decorator, then the method can chain off the |
You're right, the DI extension library is independent of specific environments, where HttpContext might not even be available. I will think on this a bit more and maybe create a small MediatR.Extensions.Microsoft.AspNetCore library (name not final, but could work) that uses the DI library. I'm wondering why no one else ever thought about building this :) In the meantime, if you want to share any suggestions, I would be happy to hear them. |
Ok, I created a repository for this: https://github.com/Schmaga/MediatR.Extensions.Microsoft.AspNetCore It is actually my first open source project, so there might be some issues. If you like and have the time, have a look at it. Would love to hear what you think. I will close this issue here. |
Hello! The conceputal difference is very important, though.
On the other hand, if this user aborted their request when they were just querying data - who cares? Having all this in mind, I can simply do this:
Please correct me if my thoughts are wrong. |
What? Don't you use transactions? Or if you're coordinating multiple non-coordinating resources, you can use a saga. Or if you're calling an API, then yes, you don't know if the operation succeeded or not so you wrap the call in something Polly. These things have to be explicitly designed though, and depend highly on the operation you're performing, and are difficult to generalize. You can use behaviors though to get some commonality, if you like. |
@jbogard I agree that such things are difficult to generalize and should be explicitly designed. @Schmaga May I suggest an improvement for your library? Wouldn't it be better if you combine both?
And then use this as follows:
|
Suggestions and PRs are always welcome. How about you open an issue in my Repo and we discuss a solution there? |
Hi! |
Hi everyone,
I am using MediatR in an ASP.NET Core 3.1 environment and I was looking for an easy way to automatically pass the HttpContext.RequestAborted CancellationToken to the RequestHandlers.
Trying different approaches and failing, I ended up with a solution like this:
And then binding the above implementation as an IMediatR replacement:
This works fine, but seems a bit clunky to me (reflected in the name as well :)), so I was wondering if there were any easier ways to this that I did not find. I had hoped to simply use a PipelineBehavior that has the IHttpContextAccessor injected and overrides the cancellation Token from the method call. But that is not possible because the RequestHandlerDelegate does not expose any method parameters for the request and cancellationToken, so there is no way to override them this way. I also would prefer to not put them in the requests themselves.
Any ideas or did I miss an easy way?
The text was updated successfully, but these errors were encountered: