.NET Core DI, ways of passing parameters to constructor

Having the following service constructor

public class Service : IService
{
     public Service(IOtherService service1, IAnotherOne service2, string arg)
     {    
     }
}

What are the choices of passing the parameters using .NET Core IOC mechanism

services.AddSingleton<IOtherService , OtherService>();
services.AddSingleton<IAnotherOne , AnotherOne>();
services.AddSingleton<IService>(x =>
    new Service(
        services.BuildServiceProvider().GetService<IOtherService>(),
        services.BuildServiceProvider().GetService<IAnotherOne >(),
        ""));

Is there any other way ?

4 Answers
4

Leave a Comment