In the Configure
callback you are putting together a configuration which is used on all bindings and when you write multiple WhenClassHas
and In...Scope
statements you are basically overriding your own settings so all your type bindings will use the last statement cfg.WhenClassHas<BindInRequestScopeAttribute>().InRequestScope();
.
To solve this you need to use the second overload of Configure
where you get the currently bound type and based on its attributes register your type with the correct scope:
using Ninject.Infrastructure.Language; //needed for HasAttribute<T>
//...
Kernel.Bind(x => x.FromAssembliesMatching("PatentSpoiler*")
.SelectAllClasses()
.BindDefaultInterface()
.Configure((cfg, type) =>
{
if (type.HasAttribute<BindAsASingletonAttribute>())
cfg.InSingletonScope();
else if (type.HasAttribute<BindInRequestScopeAttribute>())
cfg.InRequestScope();
else
cfg.InTransientScope();
}));