Dependency injection, inject with parameters

前端 未结 5 1600
盖世英雄少女心
盖世英雄少女心 2020-12-08 03:48

I\'m using vNext implementation of DI. How to pass parameters to constructor? For example, i have class:

public class RedisCacheProvider : ICacheProvider
{
         


        
5条回答
  •  难免孤独
    2020-12-08 04:42

    You can either provide a delegate to manually instantiate your cache provider or directly provide an instance:

    services.AddSingleton(provider => new RedisCacheProvider("myPrettyLocalhost:6379"));
    
    services.AddSingleton(new RedisCacheProvider("myPrettyLocalhost:6379"));
    

    Please note that the container will not explicitly dispose of manually instantiated types, even if they implement IDisposable. See the ASP.NET Core doc about Disposal of Services for more info.

提交回复
热议问题