I see defaut template use ServiceProvder.GetService<ApplicationDbCotnext>()
to initialize a DbContext,
But when you inside a Static Method, I have no idea how to get a DbContext
, because there is no ServiceProvider
.
Is there a way to get the ServiceProvider
?

- 716
- 1
- 7
- 24
-
Does this answer your question? [Dependency Injection on static method in ASP .Net Core](https://stackoverflow.com/questions/56333307/dependency-injection-on-static-method-in-asp-net-core) – Michael Freidgeim Nov 22 '22 at 06:23
2 Answers
Well, first of all, this has nothing to do with asp.net-core per se. This has more to do with how Dependency Injection works. You have to ask yourself why your method is static. Is that really necessary?
If you can't get rid of your static method, you might as well go all the way and introduce another anti-pattern, the Service Locator Pattern. In short: In the Startup class you put a reference to the ServiceProvider in a static property (call it for instance "ServiceProviderSingleton") of a static class (for instance "ServiceProviderProvider"). This way you can just call "ServiceProviderProvider.ServiceProviderSingleton.GetService()".
Again, i suggest giving your overal design a critical look. But if this is what you need/want then I hope it helped.

- 5,344
- 6
- 31
- 41
-
I find a way to access service provider; `var HttpContext = new Microsoft.AspNet.Http.Internal.HttpContextAccessor().HttpContext; var ServiceProvider = HttpContext.ApplicationServices;` Is this a right way ? – John Mar 23 '16 at 03:19
-
2In case of a long running background task that needs to have access to the database, it seems that being able to access services is justified (either from a static class or any scenario where you're working outside of the Request pipeline). – Simon Ordo Apr 14 '16 at 20:30
-
FYI for anyone that used this in .net 2.0 and migrated to .net 2.1 you need a slight change, instead of keeping a reference to ServiceProvider in startup.cs you need to instead reference app.ApplicationServices or you will get an Object disposed error. – JohnC Jun 05 '18 at 16:18
-
1I strongly disagree with your assessment. Static method and static classes are often absolutely necessary. We have been using them from the beginning of OO programming. They are highly appropriate for sections of code that do not need state. – Dave Aug 09 '19 at 17:40
-
@Dave You disagree with what exactly? Never said that there aren't use cases for static. But in the context of this OP's question I still believe my answer is appropriate. – Danny van der Kraan Sep 09 '19 at 09:34
-
@Dave...except that the injected service what you would like to achieve is by definition "a state". – g.pickardou Apr 30 '20 at 08:30
If we have a look at Microsoft's static methods (extension) - they seem not to use logging there - just throw appropriate Exception, for example in UseMvc method (for StartUp class):

- 1,076
- 12
- 19
-
this is incorrect , the extension method only throw exception when the pararmter is null or invalid. – John Nov 23 '22 at 07:07