Reputation: 13
I would like to use Microsoft's dependency injection extension to achieve DI. I have created a simple console application to test out my understanding. I have 2 objects, Dog and Cat both have a interface IAnimal. Very simple setup. I wanted to use AddTranient<IAnimal, Dog> and AddTranient<IAnimal, Cat> to create the service that houses my Dog and Cat object. Later when I retrieve objects, I am running into issues using GetRequiredService(). It only returns the last object Cat. See below code using .Net 6. How do I retrieve Dog object when I needed and vise versa for Cat object?
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
builder.Services.AddTransient<IAnimal, Dog>();
builder.Services.AddTransient<IAnimal, Cat>();
using IHost host = builder.Build();
var myobject = GetAnimalObject(host.Services, "Scope 1");
Console.WriteLine($"Name: {myobject.name}");
await host.RunAsync();
static IAnimal GetAnimalObject(IServiceProvider services, string scope)
{
using IServiceScope serviceScope = services.CreateScope();
IServiceProvider provider = serviceScope.ServiceProvider;
var result = provider.GetRequiredService<IAnimal>();
return result;
}
public sealed class Dog : IAnimal
{
public string name { get; set; } = "Daug";
}
public sealed class Cat : IAnimal
{
public string name { get; set; } = "Kate";
}
public interface IAnimal
{
string name { get; set; }
}
I tried GetRequiredService and GetRequiredService but both returned null.
Upvotes: 1
Views: 210
Reputation: 457137
It's normal for .NET's DI to return the last registered service. This allows other services to "override" the previous services.
To get all services for an interface, call GetServices<IAnimal>
instead of GetRequiredService<IAnimal>
. GetServices
will return an IEnumerable<IAnimal>
of all the services.
Upvotes: 1