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

猜你喜欢

转载自www.cnblogs.com/flames/p/11284528.html