简体   繁体   中英

adding generic services in asp.net core DI

in asp.net core DI, with the following:

public interface IRepository<T> where T:class {}
public class Repository<T> : IRepository<T> where T:class {}

I'll do the following to register my sevices:

services.AddScoped(typeof(IRepository<>), typeof(Repository<>));

but now I have my service like the the following:

public interface IContextProvider<T1,T2> {}
public class ContextProviderOne<T2> : IContextProvider<ContextClass1,T2> { }
public class ContextProviderTwo<T2> : IContextProvider<ContextClass2,T2> { }

how will I add my services to the DI?

Have you tried :

services.AddScoped(typeof(IContextProvider<T1>), typeof(ContextProviderOne<T1>));
services.AddScoped(typeof(IContextProvider<T2>), typeof(ContextProviderTwo<T2>));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM