|
14 | 14 | using Microsoft.AspNetCore.Http.Connections.Internal.Transports;
|
15 | 15 | using Microsoft.AspNetCore.Http.Features;
|
16 | 16 | using Microsoft.AspNetCore.Internal;
|
| 17 | +using Microsoft.Extensions.DependencyInjection; |
17 | 18 | using Microsoft.Extensions.Logging;
|
18 | 19 | using Microsoft.Extensions.Primitives;
|
19 | 20 |
|
@@ -537,8 +538,7 @@ private async Task<bool> EnsureConnectionStateAsync(HttpConnectionContext connec
|
537 | 538 | var existing = connection.HttpContext;
|
538 | 539 | if (existing == null)
|
539 | 540 | {
|
540 |
| - var httpContext = CloneHttpContext(context); |
541 |
| - connection.HttpContext = httpContext; |
| 541 | + CloneHttpContext(context, connection); |
542 | 542 | }
|
543 | 543 | else
|
544 | 544 | {
|
@@ -606,7 +606,7 @@ private static void CloneUser(HttpContext newContext, HttpContext oldContext)
|
606 | 606 | }
|
607 | 607 | }
|
608 | 608 |
|
609 |
| - private static HttpContext CloneHttpContext(HttpContext context) |
| 609 | + private static void CloneHttpContext(HttpContext context, HttpConnectionContext connection) |
610 | 610 | {
|
611 | 611 | // The reason we're copying the base features instead of the HttpContext properties is
|
612 | 612 | // so that we can get all of the logic built into DefaultHttpContext to extract higher level
|
@@ -660,14 +660,13 @@ private static HttpContext CloneHttpContext(HttpContext context)
|
660 | 660 |
|
661 | 661 | CloneUser(newHttpContext, context);
|
662 | 662 |
|
663 |
| - // Making request services function property could be tricky and expensive as it would require |
664 |
| - // DI scope per connection. It would also mean that services resolved in middleware leading up to here |
665 |
| - // wouldn't be the same instance (but maybe that's fine). For now, we just return an empty service provider |
666 |
| - newHttpContext.RequestServices = EmptyServiceProvider.Instance; |
| 663 | + connection.ServiceScope = context.RequestServices.CreateScope(); |
| 664 | + newHttpContext.RequestServices = connection.ServiceScope.ServiceProvider; |
667 | 665 |
|
668 | 666 | // REVIEW: This extends the lifetime of anything that got put into HttpContext.Items
|
669 | 667 | newHttpContext.Items = new Dictionary<object, object>(context.Items);
|
670 |
| - return newHttpContext; |
| 668 | + |
| 669 | + connection.HttpContext = newHttpContext; |
671 | 670 | }
|
672 | 671 |
|
673 | 672 | private async Task<HttpConnectionContext> GetConnectionAsync(HttpContext context)
|
|
0 commit comments