netcore mvc 依赖注入带参构造函数

public class DC_UserService: DBService<ApplicationUser>, IAdminService
    {
        public DC_UserService(ConnectionConfig config) : base(config)
        {
        }
}

Startup中加入代码:

services.AddTransient<IAdminService>(x => new DC_UserService(DCConnectionConfig()));

参考以下代码(解决):

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

     }
}


_serviceCollection.AddSingleton<IService>(x => 
    new Service(x.GetRequiredService<IOtherService>(),
                x.GetRequiredService<IAnotherOne>(), 
                ""));

参考问题来源:https://stackoverflow.com/questions/53884417/net-core-di-ways-of-passing-parameters-to-constructor

posted @ 2019-08-01 18:39  Flame丶  阅读(1386)  评论(1)    收藏  举报