2017-02-22 7 views
2

に汎用サービスを追加:は以下で、asp.netコアDIでasp.netのコアDI

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

私は私のsevicesを登録するには、次の手順を実行します:

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にどのように追加しますか?

+1

を 'services.AddScoped(typeof演算(IRepository <>)、typeof演算(IRepository <>))がされる方法;' 'IRepository <、動作するようになっ>'することはできませんインスタンス化された、それは具体的なクラスではなくインターフェースです。 – Tseng

+0

次の登録は組み込みコンテナでは機能しません。 'services.AddScoped(typeof(IContextProvider <,>)、typeof(ContextProviderOne <>)); services.AddScoped(typeof(IContextProvider <,>)、typeof(ContextProviderTwo <>)); '? – Steven

+1

@Tseng:それは明らかにタイプミスです。 – Steven

答えて

0

あなたが試してみました:

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