Ninject Set Default to Request Scope When Using Convention Based Binding

In many cases when using ninject on an ASP.NET site it makes sense to configure ninject to use Request Scope for its default object scope. (more info: https://github.com/ninject/ninject/wiki/Object-Scopes)

When searching for how to set Request Scope as the default object scope, I could not find any code snippets to accomplish this.

I found many snippets that would set scope for an individual binding:

kernel.Bind<IService>().To<Service>().InRequestScope();

But no examples of how to set request scope as the default for all bindings.

Here is a code snippet of how I accomplished this:

        kernel
            .Bind(i => i.FromThisAssembly()
            .SelectAllClasses()
            .BindDefaultInterface()
            .Configure(x => x.InRequestScope()));


Hope this helps,
Aaron

Comments

Popular posts from this blog

Search iPhone Text Messages with SQLite SQL Query

How to Turn Off Microsoft Arc Touch Mouse Scroll Sound Vibration

Configure SonarAnalyzer.CSharp with .editorconfig, no need for SonarCloud or SonarQube