gpt4 book ai didi

c# - 使用寄存器类型 Ninject 到 Unity 获取单实例 Controller 错误

转载 作者:太空宇宙 更新时间:2023-11-03 10:41:24 28 4
gpt4 key购买 nike

我有 Ninject 并配置了大量映射。我现在开始使用 Unity,并使用下面的代码创建所有映射:

        container.RegisterTypes(
AllClasses.FromLoadedAssemblies(),
WithMappings.FromMatchingInterface,
WithName.Default,
/* Cant remember what I was using here */);

但是现在,当我运行我的 MVC 5 项目时,我遇到了这个错误:

A single instance of controller cannot be used to handle multiple requests. If a custom controller factory is in use, make sure that it creates a new instance of the controller for each request.

最佳答案

这段代码似乎可以解决问题:

container.RegisterTypes(
AllClasses.FromLoadedAssemblies(),
WithMappings.FromMatchingInterface,
WithName.Default,
WithLifetime.Transient);

关于c# - 使用寄存器类型 Ninject 到 Unity 获取单实例 Controller 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25220610/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com