Linked Questions
34 questions linked to/from Resolving instances with ASP.NET Core DI from within ConfigureServices
0
votes
1
answer
3k
views
How can I call a repository in Startup.cs? [duplicate]
I want to grab a value from the database in my Startup.cs file through a repository. However the service is not able to be resolved.
I understand that I need to create an instance of my repository ...
173
votes
10
answers
153k
views
How and where to call Database.EnsureCreated and Database.Migrate?
I have a ASP.NET Core 6 MVC application, and I need to call the Database.EnsureCreated and Database.Migrate methods.
But where should I call them?
166
votes
7
answers
108k
views
How to resolve IOptions instance inside ConfigureServices?
Is it possible to resolve an instance of IOptions<AppSettings> from the ConfigureServices method in Startup? The documentation explicitly says:
Don't use IOptions<TOptions> or ...
133
votes
7
answers
254k
views
How to get an instance of IServiceProvider in .NET Core?
IServiceProvider is an interface with single method:
object GetService(Type serviceType);
It's used to create instances of types registered in .NET Core native DI container.
An instance of ...
125
votes
7
answers
98k
views
Dependency Injection with classes other than a Controller class
At this point I'm injecting things into my Controllers with ease, in some cases building my own ResolverServices class. Life is good.
What I cannot figure out how to do is get the framework to ...
59
votes
5
answers
68k
views
How to inject or use IConfiguration in Azure Function V3 with Dependency Injection when configuring a service
Normally in a .NET Core project I would create a 'boostrap' class to configure my service along with the DI registration commands. This is usually an extension method of IServiceCollection where I can ...
28
votes
3
answers
32k
views
Net Core: Execute All Dependency Injection in Xunit Test for AppService, Repository, etc
I am trying to implement Dependency Injection in Xunit test for AppService. Ideal goal is to run the original application program Startup/configuration, and use any dependency injection that was in ...
29
votes
4
answers
18k
views
Net Core Dependency Injection for Non-Controller
Seems crazy that something like this is causing me such a headache. But here it is:
How do you use the built-in dependency injection for net core for a non-controller class? Please provide an ...
25
votes
3
answers
49k
views
ASP.NET Core JWT Bearer Token Custom Validation
After a lot of reading, I have found a way to implement a custom JWT bearer token validator as below.
Starup.cs:
public void Configure(IApplicationBuilder app, IHostingEnvironment env,
...
26
votes
2
answers
24k
views
.net core resolve dependency manually anywhere in the code
Do you know how to manually resolve dependencies in .net core?
Something like
DependencyResolver.Resolve<ISomeService>()
UPDATE
I'm in a class that was not injected, I want to resolve it from ...
6
votes
2
answers
4k
views
DBContext System.ObjectDisposed Exception with .NET Entity Framework Core, Dependency Injection and threading
I am not sure if I am going about this correctly at all.
Background:
I have a controller action GET foo() as an example. This foo() needs to go and call bar(), and bar() may take a verrrrrry long ...
13
votes
1
answer
26k
views
.Net Core Dependency Injection inject out of constructor
I need to inject out of constructor, everything I declared in Setup.
Ho can I do it ? How can I inject services out of constructor ?
Something like Injector service in Angular 2.
INJECT SERVICES ...
7
votes
2
answers
5k
views
Can't use registered singetons in ConfigureServices without them being instantiated twice
I have a .Net Core project that registers a number of Singletons as follows:
public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache();
services.AddLogging();
...
0
votes
2
answers
4k
views
ASP.NET Core dependency injection - How to create instances?
In Startup:
services.AddTransient<IMyService, MyService>()
Controller method:
[HttpGet]
public JsonResult GetSomething()
{
Helper helper = new Helper(new MyService()); // works but looking ...
2
votes
1
answer
2k
views
Dependency injection with a .Net Class Library?
I have a Class library that does a lot of File IO. Its a bit difficult to test so I wanted to start using the System.IO.Abstractions package. It has an interface that you can implement with either the ...