1

Before I begin, I'm aware the Service Locator pattern is not recommended and it's better to inject services into the classes where they are used. However, I have a large legacy codebase that creates a lot of objects inside methods without DI. Refactoring this would be a monumental task so I am trying to implement a static Service Locator as reasonably as possible.

My idea is to create a scope as part of a request middleware and set the ServiceLocator instance here. Then allow it to flow through the rest of the request chain before it exists and disposes. I have tested this and my created class is disposed, so it seems to be working as expected. Are there any pitfalls I haven't considered?

public class DependencyLocatorHandler(RequestDelegate next, IServiceProvider serviceProvider)
{
 public async Task InvokeAsync(HttpContext context)
 {
 await using var serviceProviderScope = serviceProvider.CreateAsyncScope();
 ServiceLocator.SetServiceProvider(serviceProviderScope.ServiceProvider);
 await next(context);
 }
}
public static class ServiceLocator
{
 private static IServiceProvider _serviceProvider;
 public static void SetServiceProvider(IServiceProvider serviceProvider)
 {
 _serviceProvider = serviceProvider;
 }
 public static T GetService<T>()
 {
 return _serviceProvider.GetService<T>();
 }
}
asked Apr 23 at 4:18
6
  • 2
    Will not work for multithreading. Commented Apr 23 at 8:40
  • And accessing any sort of shared state from a static context will kill you application's ability to serve multiple requests by the same user concurrently if you have IIS configured for sticky sessions. Commented Apr 23 at 10:57
  • I was worried about this. But what other option do I have? Commented Apr 23 at 12:05
  • Can you use OWIN -- is your application old enough to use that? I think the trick here is to initialize the service locator as an object at the beginning of your pipeline. Then each middleware component can pull it out of the current HTTP context. Commented Apr 23 at 12:22
  • No, it's dotnet 9. Commented Apr 23 at 21:58

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.