2

は、私は次の要求ハンドラを持っている:Simple Injectorを使用してネストされたジェネリックタイプを登録するにはどうすればよいですか? Mediatrを使用して

public class GetEntityByIdRequest<TEntity> : IRequest<TEntity> where TEntity : Entity 
{ 
    public int Id { get; set; } 

    internal class Handler : IRequestHandler<GetEntityByIdRequest<TEntity>, TEntity> 
    { 
     public TEntity Handle(GetEntityByIdRequest<TEntity> message) 
     { 
      return new Session.Query<TEntity>().FirstOrDefault(x => x.Id == message.Id); 
     } 
    } 
} 

私はトラブル私のIoCで、この一般的なリクエストハンドラを登録したのです。私のような登録を試してみました:

container.Register(typeof(IRequestHandler<,>), typeof(GetEntityByIdRequest<>)); 
container.Register(typeof(IRequestHandler<,>), typeof(GetEntityByIdRequest<Entity>)); 

私にエラーを与える:

System.ArgumentException : The supplied type GetEntityByIdRequest<TEntity> does not implement IRequestHandler<TRequest, TResponse>. Parameter name: serviceType

は対照的に、私はまた私が午前問題と同一であるthis見てきましたが、人はのStructureMapを使用していますシンプルインジェクターへ

ジェネリックリクエストハンドラの登録を手伝ってもらえますか?

答えて

2

クエリタイプをハンドブックとして登録しています。これは明らかに機能しません。代わりにハンドラを登録する必要があります:

container.Register(typeof(IRequestHandler<,>), typeof(GetEntityByIdRequest<>.Handler)); 
+0

はこれを試し、それは私にエラーを与えている: System.NotSupportedException:「AllowOverridingRegistrationsが設定されている場合、それはのために不可能であるため、条件付き登録の意思が、サポートされていません。登録が異なる登録を置き換えるべきかどうかを検出するためのコンテナ。一般的なタイプの制約のため、登録は条件付きとみなされます。これにより、シンプルインジェクタはその型制約に基づいて条件付きで適用されます。 – Viqas

+0

例外状態として 'Container.Options.AllowOverridingRegistrations'が' false'であることを確認してください。 – Steven

+1

これを動作させるには、ハンドラクラスをpublicにする必要があります。 – TomJerrum

関連する問題