我在使用InSingletonScope()时遇到了一些问题。
我的界面:
public interface ISettingsManager
{
ApplicationSettings Application { get; }
}
和我的班级:
public class SettingsManager : ISettingsManager
{
private readonly IConfigurationService _configurationService;
private readonly Lazy<ApplicationSettings> _applicationSettings;
public ApplicationSettings Application { get { return _applicationSettings.Value; } }
private SettingsManager(IConfigurationService context)
{
_configurationService = context;
_applicationSettings = new Lazy<ApplicationSettings>(() => new ApplicationSettings(context));
}
}
和NinjectWebCommon中的标准绑定如下所示:
kernel.Bind<ISettingsManager>().To<SettingsManager>().InSingletonScope();
kernel.Bind<IConfigurationService >().To<ConfigurationService>().InRequestScope();
当我在HomeController中使用构造函数注入或属性注入时:
[Inject]
public ISettingsManager SettingsManager { private get;}
然后我收到错误:
An error occurred when trying to create a controller of type 'Web.Controllers.HomeController'. Make sure that the controller has a parameterless public constructor.
问题出在哪里?我的单身人士出了什么问题?
我在项目依赖注入中使用,当我在构造函数中注入一个接口时,一切正常。当我添加ISettingsManagers时,我遇到了很多问题。
答案 0 :(得分:1)
我知道出了什么问题。私有构造函数是主要问题。当我将其更改为:
public SettingsManager(IConfigurationService context)
然后它就像一个魅力。
答案 1 :(得分:0)
错误它自己说它的家庭控制器需要无参数,但是如果ninject设置正确并且不是真的,并且bug可以嵌套到没有所需绑定的单个类。
但是,你在这里展示的内容有一件事可能会出错。
您的配置在请求范围内,您的设置在单一范围内。
这意味着它可能在第一次运行正常,但是在第二次尝试中,它的新请求和配置可能会被放置在您的单例设置中,并且可能会破坏某些内容。