繁体   English   中英

在asp.net核心DI中添加通用服务

[英]adding generic services in asp.net core DI

在asp.net核心DI中,具有以下内容:

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

我将执行以下操作来注册我的服务:

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

但是现在我的服务如下:

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

如何将服务添加到DI中?

你有没有尝试过 :

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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