The ServiceCollection in middleware can be overridden by creating a new instance of it in the Configure method of your Startup.cs class. This new instance can be populated with your own services that will replace the default implementation. For example:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
var serviceCollection = new ServiceCollection();
// Register your own services here
serviceCollection.AddTransient<IMyService, MyService>();
// Replace the default services with your own
app.ApplicationServices = serviceCollection.BuildServiceProvider();
app.UseMiddleware<MyMiddleware>();
}
Note that the ApplicationServices property of the app parameter is being set to the new ServiceProvider created from the ServiceCollection. This will ensure that any service resolution inside the middleware pipeline will use the new service instances instead of the defaults.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-08-30 11:00:00 +0000
Seen: 16 times
Last updated: Jul 28 '21
What does "terminate called after throwing an instance of 'std::out_of_range'" mean?
What is the method to obtain the current DecoderVideoRenderer in Android ExoPlayer 2?
How can the Django post_save() signal be implemented?
What is the process for transferring image files to an AWS EC2 instance?
What is the issue with calling `jakarta.validation.Validation#buildDefaultValidatorFactory`?
How can I run the same executable as the present instance in C# without using Process.Start()?
When a fragment is destroyed, why isn't the Android ViewModel also destroyed?