无法重用 'ApiController' 实例 - Unity DI
Cannot reuse an 'ApiController' instance - Unity DI
我按照 this 教程在 WebAPI 中使用 Unity 依赖注入。
为了避免分别注册每种类型,我使用了这个设置
_container.RegisterTypes(
AllClasses.FromLoadedAssemblies(),
WithMappings.FromMatchingInterface,
WithName.Default,
WithLifetime.ContainerControlled);
这第一次很好用,但自从第二次我得到
"Cannot reuse an 'ApiController' instance. 'ApiController' has to be constructed per incoming message. Check your custom 'IHttpControllerActivator' and make sure that it will not manufacture the same instance."
我该如何重新爱这个?
从表面上看,当您说 WithLifetime.ContainerControlled
时,这意味着您要求 Unity 注册一个 Singleton 实例(即重用该实例),这正是它所抱怨的。
您需要设置一个 PerRequest 生命周期管理器
我按照 this 教程在 WebAPI 中使用 Unity 依赖注入。
为了避免分别注册每种类型,我使用了这个设置
_container.RegisterTypes(
AllClasses.FromLoadedAssemblies(),
WithMappings.FromMatchingInterface,
WithName.Default,
WithLifetime.ContainerControlled);
这第一次很好用,但自从第二次我得到
"Cannot reuse an 'ApiController' instance. 'ApiController' has to be constructed per incoming message. Check your custom 'IHttpControllerActivator' and make sure that it will not manufacture the same instance."
我该如何重新爱这个?
从表面上看,当您说 WithLifetime.ContainerControlled
时,这意味着您要求 Unity 注册一个 Singleton 实例(即重用该实例),这正是它所抱怨的。
您需要设置一个 PerRequest 生命周期管理器