-
Notifications
You must be signed in to change notification settings - Fork 1.7k
#2178 DI service resolution from scoped HttpContext request services for the IConsulServiceBuilder service
#2179
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
#2178 DI service resolution from scoped HttpContext request services for the IConsulServiceBuilder service
#2179
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the high-quality PR. I suggest writing acceptance tests, if applicable.
| contextAccessor.HttpContext.Items[nameof(ConsulRegistryConfiguration)] = configuration; // initialize data | ||
| var serviceBuilder = provider.GetService<IConsulServiceBuilder>(); // consume data in default/custom builder | ||
| context.Items[nameof(ConsulRegistryConfiguration)] = configuration; // initialize data | ||
| var serviceBuilder = context.RequestServices.GetService<IConsulServiceBuilder>(); // consume data in default/custom builder |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does reading context services implicitly create a scope? I'll demonstrate the difference in the following code:
- var scope = provider.CreateScope();
- var serviceBuilder = scope.ServiceProvider.GetService<IConsulServiceBuilder>(); // consume data in default/custom builder
+ var context = contextAccessor.HttpContext;
+ var serviceBuilder = context.RequestServices.GetService<IConsulServiceBuilder>(); // consume data in default/custom builderThus, are the two solutions equivalent?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK ASP.NET creates a DI scope for HttpContext of each HTTP request (see this link for more info).
Technically, the solutions are equivalent, but I thought that since we already access HttpContext via IHttpContextAccessor, it would be logical to access the scope of the HTTP request instead of creating a new one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see this link for more info
Yep, good to know!
Bu gaga, Tom should read these best DI practices in 2017-2020 when designing everything as singletons and delegates → Design services for dependency injection
🆗 Not an issue. Your solution should function properly.
HttpContext request services for the IConsulServiceBuilder service
|
Please ask me to initiate new builds. Are you not able to click the buttons in the Checks? 🤔 |
Okay, will do. There was a test that was flaky, so I just triggered another run by the only way I know ;) |
|
IMO new acceptance tests are not required here:
|
Fair enough, but how many tests would break if we bypass the parameter and enforce scope validation for all tests? It would be logically preferable to resolve all issues with scoped services in one PR, but that seems to be beyond the current PR's scope. Tomorrow, I plan to conduct research to determine how to comprehensively address all issues with scoped services... |
And file name should match class name.
|
I've identified numerous instances where scope validation is disabled in unit tests: specifically, a search for ".BuildServiceProvider(" reveals about 25 files with manual construction of service collections without validation. Unfortunately, we cannot address these in the current PR. 😢 But the final number of items ~50+ 🤯 |
|
Regrettably, the search for "WebHostBuilder" revealed a significant issue with disabled scopes validation in tests and the core library 🤯 This wasn't an issue when services were primarily designed as singletons by Tom and contributors. However, as we began to introduce more scoped services, it became apparent that our testing projects were not equipped to handle them. The subsequent task will be to enable scope validation across all areas: unit tests, acceptance tests, and the core library. At this time, it is not feasible to tackle this extensive problem within this PR. During refactoring, it is sensible to:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Development Complete❕
|
@Niksson This PR has been merged. |
* OcelotBuilderExtensionsTests for Eureka SD * BuildServiceProvider(true) #2179 (comment) * Add TestHostBuilder with enabled scopes validation by default across all testing projects #2179 (comment) * Update Sample projects with force scopes validation. Add Ocelot.Samples.Web project. Add OcelotHostBuilder and DownstreamHostBuilder helpers. Add Ocelot.Samples.Web project reference to all samples projects. TargetFramework is `net8.0` only.
Fixes #2178
ConsulProviderFactorycan't resolveIConsulServiceBuilderviaIServiceProvider#2178This fixes the issue with DI service resolution for
Consulprovider mentioned in #2178Proposed Changes
ConsulProviderFactory- resolve theIConsulServiceBuilderfrom theHttpContext.RequestServicesConsulProviderFactorySome notes:
ConsulProviderFactory.Getcreates bothConsulandPollConsulproviders, so the fix should cover both of themIf there is anything that can be changed to improve quality - do not hesitate to propose changes
Docs