1

Unity でサンプルの WCF 依存性注入を実装しようとすると、このエラーが発生します。

Resolution of the dependency failed, type = "SampleServiceUsingUnity.ExampleService", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type,    ExampleServiceUsingUnity.IExampleManager, is an interface and cannot be constructed. Are you missing a type mapping?
-----------------------------------------------
At the time of the exception, the container was:
Resolving SampleServiceUsingUnity.ExampleService,(none)
Resolving parameter "exampleManager" of constructor     SampleServiceUsingUnity.ExampleService(SampleServiceUsingUnity.IExampleManager exampleManager)
Resolving SampleServiceUsingUnity.IExampleManager,(none)

問題は、型を明示的にマップしようとしたことです...そして、コードをデバッグするときに、すぐにウィンドウに移動して登録を確認します。下記参照:

**DependencyFactory.Container.Registrations.ToList()[2]**

{Microsoft.Practices.Unity.ContainerRegistration}
buildKey: {Build Key[SampleServiceUsingUnity.IExampleRepository, null]}
LifetimeManager: {Microsoft.Practices.Unity.ContainerControlledLifetimeManager}
LifetimeManagerType: {Name = "ContainerControlledLifetimeManager" FullName = "Microsoft.Practices.Unity.ContainerControlledLifetimeManager"}
MappedToType: {Name = "ExampleRepository" FullName = "SampleServiceUsingUnity.ExampleRepository"}
Name: null
RegisteredType: {Name = "IExampleRepository" FullName = "SampleServiceUsingUnity.IExampleRepository"}

したがって、マッピングは正しいように見えますが、サービスが Resolve を使用してそのインターフェイスをインスタンス化しようとすると、エラーが発生します。

コード例は、http: //msdn.microsoft.com/en-us/library/hh323725.aspxで提供されています。

4

1 に答える 1

1

例外はIExampleManagerについてであり、登録はIExampleRepositoryについてです。マネージャのマッピングが欠落していて、代わりにリポジトリにマッピングを提供している可能性はありますか?

于 2012-05-14T12:38:50.790 に答える