Ninject + ASP.NET MVC + InRequestScope

I have a problem with Ninject.

My binding rules:

this.Bind<ISphinxQLServer>().To<SQLServer>(); this.Bind<IMySQLServer>().To<SQLServer>(); this.Bind<ISQLLogger>().To<StandardSQLLogger>() .InRequestScope(); this.Bind<DatabaseConnections>() .ToMethod(x => ConnectionFactory.GetConnections()) .InRequestScope(); this.Bind<SQLServer>().ToSelf() .InRequestScope() .WithConstructorArgument("connections", Kernel.Get<DatabaseConnections>()) .WithConstructorArgument("logger", Kernel.Get<ISQLLogger>()); 

Where

SQLServer, ISphinxQLServer and IMySQLServer:

 public class SQLServer: ISphinxQLServer, IMySQLServer { public DatabaseConnections Connections { get; internal set; } public ISQLLogger Logger { get; internal set; } public SQLServer(DatabaseConnections connections) { this.Connections = connections; } public SQLServer(DatabaseConnections connections, ISQLLogger logger) { this.Connections = connections; this.Logger = logger; } } 

I want each user request to my asp.net mpc site to create one SQLServer, one ISQLLogger and one DatabaseConnections. But my solution does not work. What am I doing wrong? = (

+6
asp.net-mvc ninject
source share
1 answer

You do not need to specify WithConstructorArgument . Allowing parameters on the constructors of your injected objects is part of what Ninject does for you. Therefore, the definitions should look like this:

 this.Bind<SQLServer>() .ToSelf() .InRequestScope(); this.Bind<ISphinxQLServer>() .ToMethod( x => x.Kernel.Get<SQLServer>() ); this.Bind<IMySQLServer>() .ToMethod( x => x.Kernel.Get<SQLServer>() ); this.Bind<ISQLLogger>() .To<StandardSQLLogger>() .InRequestScope(); this.Bind<DatabaseConnections>() .ToMethod(x => ConnectionFactory.GetConnections()) .InRequestScope(); 
+6
source share

All Articles