On .net core 3.1 Startup.cs
Trying to get the instance of already registered type i.e. "IBusinessLogic" using "IServiceCollection", it is not working.
How to get the instance of already registered type in .net core 3.1?
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
container.Register<IBusinessLogic, BusinessLogic>();
container.AddSingleton<Func<string, string>>
((username, password) => new JWTCache(userId, password,
container.GetInstance<IBusinessLogic>())); //container.GetInstance<IBusinessLogic>() not working
}
}
>Solution :
You need to use the overload that gives you an IServiceProvider:
container.AddSingleton<Func<string, string>>(
sp => (username, password) => new JWTCache(userId, password, sp.GetRequiredService<IBusinessLogic>())
);