1

How can I resolve instances without injection in asp net core

When we used spring .NET we could get an instance by something like:

StaticFactory.GetInstance<T>(); 

1 Answer 1

3

In ASP.NET Core, services are available within a request from HttpContext:

HttpContext.RequestServices.GetService(typeof(MyService)); 

Docs: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.2#request-services

Prefer requesting dependencies as constructor parameters to accessing the RequestServices collection.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! (7 more chars)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.