0

以下のAutofacコンテナをテストしています:

var builder = new ContainerBuilder();

builder.Register(t => new TreatmentCenterRepository())
  .As<IRepository<TreatmentCenter>>();

builder.Register(t => new CreateTreatmentCenterCommandHandler(t.Resolve<IRepository<TreatmentCenter>>()))
  .As<ICommandHandler<CreateTreatmentCenterCommand>>();
var container = builder.Build();
var repo = container.Resolve<IRepository<TreatmentCenter>>();
var handler = container.Resolve<ICommandHandler<TreatmentCenter>>();

Console.WriteLine(repo);
Console.WriteLine(handler);

コマンド ハンドラーの実装には、リポジトリ パラメーターを持つ 1 つの ctor があります。

これを実行すると、次の例外が発生します。

Unhandled Exception: Autofac.Core.Registration.ComponentNotRegisteredException: The requested service 'Console
Application2.ICommandHandler`1[[ConsoleApplication2.TreatmentCenter, ConsoleApplication2, Version=1.0.0.0, Cul
ture=neutral, PublicKeyToken=null]]' has not been registered.
   at Autofac.ResolutionExtensions.Resolve(IComponentContext context, Service service, IEnumerable`1 parameter
s)
   at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context, IEnumerable`1 parameters)
   at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context)
   at ConsoleApplication2.Program.Main(String[] args) in D:\Projects\Test Projects\ConsoleApplication2\Console
Application2\Program.cs:line 30

なぜバーフィングなのですか?そのハンドラーをctor paramとしてリポジトリに明確に登録しました。

ありがとう

4

1 に答える 1

2

ICommandHandler<TreatmentCenter>例外メッセージは、コンテナに登録されていない問題を解決しようとしていることを示しています。コンテナに入っているのはですICommandHandler<CreateTreatmentCenterCommand>。もしかしてタイプミスですか?

于 2010-05-13T13:29:57.070 に答える