0
votes

I have some problem with InSingletonScope().

My interface:

public interface ISettingsManager
    {
        ApplicationSettings Application { get; }
    }

and my class:

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));
        }
}

and standard binding in NinjectWebCommon looks like this:

kernel.Bind<ISettingsManager>().To<SettingsManager>().InSingletonScope();
kernel.Bind<IConfigurationService >().To<ConfigurationService>().InRequestScope();

And when I use constructor injection or property injection in HomeController:

    [Inject]
    public ISettingsManager SettingsManager { private get;}

Then I get an error:

An error occurred when trying to create a controller of type 'Web.Controllers.HomeController'. Make sure that the controller has a parameterless public constructor.

Where is the problem? What is wrong with my Singleton?

I use in my project dependency injection and when I inject one interface in constructor, everything work fine. When I add ISettingsManagers I have many problems.

2
1) Have you made sure your controllers are being added to the container, i.e. have you specified GlobalConfiguration.DependencyResolver, 2) Why do you have property injection AND constructor injection on the same controller - or are you passing in some random arguments to your constructor.Callum Linington
1) How I can specify GlobalConfiguration.DependencyResolver ? I use only standard NinjectWebCommon. 2) In my Home Controller I have some service injection (InRequestScope) and this is working fine. When I add ISettingsManager in second parameter in Home Controller (or in the property injection) I always get the error with parameterless public constructoruser3146344
show your NinjectWebCommon unless it looks like this link. Are you in WebApi or MVC or both?Callum Linington
@RuaidhriPrimrose the answer to that question is to bind it to an interface - this one already is....Callum Linington

2 Answers

1
votes

I know what was wrong. The private constructor was the main problem. When I change it to:

public SettingsManager(IConfigurationService context)

then it works like a charm.

0
votes

The Error it self says its the home controller that needs to be parameterless, however if ninject is setup correctly that is not true, and the bug can be nested down to a single class that does not have the bindings needed.

However, there is one thing that might go wrong in what you show here.

Your configuration is in request scope and your settings is in singleton scope.

This means it might run fine the first time, however in the second attempt, its a new request and the configuration, could be disposed inside your singleton settings, and would proberbly break something.