Fix #2294: batch processing loses HttpContext for main request.#2385
Fix #2294: batch processing loses HttpContext for main request.#2385NicholasNoise wants to merge 436 commits into
Conversation
1.) Switch to ps1 script to execute build, tests, and display results.
2.) Delete unused MSBuild files.
3.) Remove xunit.execution.dotnet from the AspNet UnitTest project since it's desktop, not dotnet.
4.) Repath UT and E2E output to AspNet and AspNetCore subfolders.
5.) Fix issue in security helper where stdErr was not checked.
6.) Modify PublicAPi test to write directly to file instead of capturing StdOut, which caused the
test to fail occasionally by adding other Console.Write()'s to the .out file.
7.) Fix duplicate test case in ODataRawValueSerializerTests.cs
1.) delete duplicate bsl files for public API and fix
instructions for updating the baseline.
2.) Diagnose (but not fix) unstable DateTime tests. I added
a note explaining what needs to be fixed in the product.
…eness in query option, operator and function names; Test additions and test adjustments.
* Fix invalid next page link in child collections * Make variable local * Added test for valid next page link generation * Implement review suggestions * Remove unnecessary else block Co-authored-by: John Gathogo <jogathogo@microsoft.com>
* Full async support * Wrap streams passed to input/output formatters to ensure only calling async methods * Move from Wait() to async. May have to expose as new method for back compat. * Improvements * Elide trailing awaits
* Enable JSON Metadata * Fix the failing test cases * address the comment
…2362) * Enabling query validation EnableQuery before action execution * Adding test * Name updates. * Adding #if for NetCore tests * Adding code to throw BadRequest * Fixing issues * Fixing the public API file * Adding fixes for tests * Fixing generic type code * Adding support for ActionResult<> * Taking PR comments * Taking PR comments * Taking PR comment * Taking PR comments * Just retrying build
* Fix MergeIndividualAndBatchPreferences method for same Prefer headers case (bug with an unnecessary comma at the end of line) * Add test merging same Prefer header while processing batch. Co-authored-by: Tsar Nikolay <nsmirnov@ics.perm.ru>
de97791 to
27ea2f1
Compare
27ea2f1 to
06a1f05
Compare
| } | ||
| // Create a context. | ||
| // IHttpContextFactory should not be used, because it resets IHttpContextAccessor.HttpContext; | ||
| HttpContext context = new DefaultHttpContext(features); |
There was a problem hiding this comment.
HttpContext context = new DefaultHttpContext(features); [](start = 12, length = 55)
What are the ramifications of not using the context factory? Doesn't that mean that, if someone adds their own context factory, we don't use it for batch? Couldn't that be a problem if the developer is expecting their custom httpcontext to be used? Do we instead need a way to create the context from the factory without resetting the IHttpContextAccessor.HttpContext?
There was a problem hiding this comment.
I do not see any other reason to use the factory for creating HttpContext from a set of sub-requests, but dependency injection. And it is unclear why a global factory (that creates context for an actual http request) should be used to instance batch sub-contexts.
If the factory is used we should make a closure of original context after processing batch changesets like this one:
/// <inheritdoc />
public override async Task ProcessBatchAsync(HttpContext context, RequestDelegate nextHandler)
{
// Retrieve current httpcontext.
var httpContextAccessor = context.RequestServices.GetService<IHttpContextAccessor>();
var beforeContext = httpContextAccessor?.HttpContext;
await base.ProcessBatchAsync(context, nextHandler);
if (httpContextAccessor != null)
{
httpContextAccessor.HttpContext = beforeContext;
}
}But still a closure is more like a workaround than THE solution.
Keyword new is kinda bad in common, especially in the name of DI, instead we can introduce an interface IBatchHttpContextFactory to create a context of the changeset (silly refactor)
There was a problem hiding this comment.
Hi @mikepizzo & @NicholasNoise, I tested the pr with the two suggestions. I guess my question would be for Sub requests created during batch processing should we give users more control over the creation of the context? Since we do copy the features, headers, and cookie information. One of the issues with the current approach I do see is if the user used their own IHttpContextAccessor and IHttpContextFactory then this process would need them to do more refactors if they needed to change how batch sub requests are created by extending the BatchMiddleware.
There was a problem hiding this comment.
Hi @marabooy
Yeah, we should give more control of the context creation. But as I've pointed out IHttpContextFactory is not a way to do so. I suggest a new abstraction IBatchHttpContextFactory for this matter.
|
Hi @NicholasNoise -- thanks for the contribution! As per my inline comments, I'm curious what the ramifications are if we don't use the injected HttpContextFactory. Eager to get your (and others') thoughts. |
|
@mikepizzo |
4c43a84 to
ddfd3dd
Compare
Issues
This pull request fixes issue #2294.
Description
IHttpContextFactory(DefaultHttpContextFactory) service should not be used to create newHttpContext, because it resets current context to new one:https://github.com/dotnet/aspnetcore/blob/master/src/Hosting/Hosting/src/Http/DefaultHttpContextFactory.cs#L73
Checklist (Uncheck if it is not completed)