ASP.NET MVC, EF and Ninject : Different ObjectContext issue - asp.net-mvc

I'm using Ninject for DI and injecting IDbContext to my repositories as constructor parameter. I get a "The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects." error while trying to do something like this:
This is my controller's action method:
public ActionResult BindSpace(int spaceId, int managerId)
{
Space space = _spaceService.GetSpace(spaceId);
Manager manager = _managerService.GetManager(managerId);
if (space != null && manager != null)
{
_spaceService.BindManager(space, manager);
}
return RedirectToAction("GetSpaceBindingForm", new { id = space.Id });
}
This is the service method:
public void BindManager(Space space, Manager manager)
{
if (space != null && manager != null)
{
space.Managers.Add(manager);
_spaceRepo.Update(space);
}
}
There was no problem while adding and updating non-related entities.
There is no problem when I use:
ninjectKernel.Bind<IDbContext>().To<SPBSObjectContext>().InSingletonScope().WithConstructorArgument("nameOrConnectionString", "ShoppingPointBrowsingSystem");
I searched the web and everyone implements and uses NinjectModule abstract base class, but I have the following code. What am I doing wrong here?
This is the injection part:
public class NinjectControllerFactory : DefaultControllerFactory
{
private IKernel ninjectKernel;
public NinjectControllerFactory()
{
ninjectKernel = new StandardKernel();
AddBindings();
}
protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
{
return controllerType == null ? null : (IController)ninjectKernel.Get(controllerType);
}
private void AddBindings()
{
// HTTP Context
ninjectKernel.Bind<HttpContextBase>().ToMethod(context => new HttpContextWrapper(HttpContext.Current));
// Context
ninjectKernel.Bind<IDbContext>().To<SPBSObjectContext>().InRequestScope().WithConstructorArgument("nameOrConnectionString", "ShoppingPointBrowsingSystem");
// Repositories
ninjectKernel.Bind<IRepository<Admin>>().To<EfRepository<Admin>>().InRequestScope();
ninjectKernel.Bind<IRepository<Manager>>().To<EfRepository<Manager>>().InRequestScope();
ninjectKernel.Bind<IRepository<ShoppingCenterSpace>>().To<EfRepository<ShoppingCenterSpace>>().InRequestScope();
ninjectKernel.Bind<IRepository<IndependentStoreSpace>>().To<EfRepository<IndependentStoreSpace>>().InRequestScope();
ninjectKernel.Bind<IRepository<Space>>().To<EfRepository<Space>>().InRequestScope();
// Services
ninjectKernel.Bind<IAuthenticationService<Admin>>().To<AdminFormsAuthenticationService>();
ninjectKernel.Bind<IAdminService>().To<AdminService>();
ninjectKernel.Bind<IManagerService>().To<ManagerService>();
ninjectKernel.Bind<IShoppingCenterSpaceService>().To<ShoppingCenterSpaceService>();
ninjectKernel.Bind<IIndependentStoreSpaceService>().To<IndependentStoreSpaceService>();
ninjectKernel.Bind<ISpaceService>().To<SpaceService>();
}
}

InRequestScope requires the Ninject.MVC3 extenison instead of an own ControllerFactory . Otherwise it behaves like InTransientScope

Related

How to use Ninject with HttpClient

What would be the recommended way to use Ninject to inject the same HttpClient object to all Controller instances in an application?
Currently, I am injecting an EntityFramework Database context following Adam Freeman's MVC book as follows. However, this creates a new dbContext for each controller instance, which is probably not ideal for HttpClient, since HttpClient is meant to be reused across all controllers in an MVC application.
Constructor:
public class AccountController : Controller
{
MyDBContext dbContext = new MyDBContext();
public AccountController(MyDBContext context)
{
dbContext = context;
}
...
}
And the Ninject Factory is as follows:
/// Class based on Adam Freeman's MVC book to use dependency injection to create controllers
public class NinjectControllerFactory : DefaultControllerFactory
{
private IKernel ninjectKernel;
public NinjectControllerFactory()
{
ninjectKernel = new StandardKernel();
AddBindings();
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
return controllerType == null
? null
: (IController)ninjectKernel.Get(controllerType);
}
private void AddBindings()
{
ninjectKernel.Bind<MyDBContext>().ToSelf().InTransientScope();
}
}
You just have to change your configuration to:
ninjectKernel.Bind<MyDBContext>().ToSelf().InRequestScope();
For more information about request scoping, please read this.
Thanks Steven. Currently, I find that the following works. I created a static HttpClient property in the NinjectController and bound it as constant in singleton scope. Daniel's book was helpful in better understanding Ninject.
/// Class based on Adam Freeman's MVC book to use dependency injection to create controllers
public class NinjectControllerFactory : DefaultControllerFactory
{
private IKernel ninjectKernel;
private static HttpClient WebAPIClient; // added
public NinjectControllerFactory()
{
ninjectKernel = new StandardKernel();
WebAPIClient = new HttpClient(); // added
WebAPIClient.BaseAddress = new Uri("http://localhost:1153"); // added
AddBindings();
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
return controllerType == null
? null
: (IController)ninjectKernel.Get(controllerType);
}
private void AddBindings()
{
ninjectKernel.Bind<MyDBContext>().ToSelf().InTransientScope();
ninjectKernel.Bind<HttpClient>().ToConstant(WebAPIClient).InSingletonScope(); // added
}
}

ArgumentNullException in WindsorControllerActivator

I have a project with multiple layers(assemblies). I used Castle Windsor to solve Dependency Injection. Also create my controllers with castle. so I installed and used castle in Web API and WebUI Layers too. Beside I have a bootstrapper layer which registers all services and repositories.
First my web API:
In WebApiConfig.cs file, I registered UI Dependencies with flowing code:
public static void Register(HttpConfiguration config)
{
var corsAttr = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(corsAttr);
// Web API configuration and services
var container = Bootstrapper.WireUp();// to register services and repositories
RegisterControllers(container);
config.Services.Replace(typeof(IHttpControllerActivator), new WindsorControllerActivator(container));
}
private static void RegisterControllers(IWindsorContainer container)
{
//container.Register(Component.For<VisitorController>().LifestylePerWebRequest());
//container.Register(Component.For<ValueController>().LifestylePerWebRequest());
//container.Register(Component.For<ValidateController>().LifestylePerWebRequest());
//container.Register(Component.For<CaptchaImageController>().LifestylePerWebRequest());
container.Register(Classes.FromAssemblyContaining<VisitorController>()
.BasedOn<ApiController>().LifestyleTransient());
}
And activator factory is like this:
public class WindsorControllerActivator : IHttpControllerActivator
{
private readonly IWindsorContainer _container;
public WindsorControllerActivator(IWindsorContainer container)
{
_container = container;
}
public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
{
return (IHttpController)_container.Resolve(controllerType);//System.ArgumentNullException: Value cannot be null. error
}
}
Also in WebUI project I used Castle Windsor to create my controllers. But when I run the project I get "System.ArgumentNullException: Value cannot be null." error when is resolving Controller.I mean in this line:
(IHttpController)_container.Resolve(controllerType);
I also tried "LifeStylePerWebRequest" for my controllers and nothing were changed.
Whats more, nothing is wrong with WireUp Method,(Registering service and repositories works fine). This problem is just for controllers(in WebUI and WebApi projects). Same problem happens for WebUI project.
I normally use implementation of IDependencyResolver instead of IHttpControllerActivator. You can this approach instead. Add the floowing class:
public class WindsorDependencyResolver : IDependencyResolver
{
readonly IWindsorContainer _container;
public WindsorDependencyResolver(IWindsorContainer container)
{
if (container == null)
{
throw new ArgumentNullException(nameof(container));
}
_container = container;
}
public IDependencyScope BeginScope()
{
return new WindsorDependencyScope(this, _container.Release);
}
public void Dispose()
{
}
public object GetService(Type t)
{
var ret = _container.Kernel.HasComponent(t) ? _container.Resolve(t) : null;
return ret;
}
public IEnumerable<object> GetServices(Type t)
{
var ret = _container.ResolveAll(t).Cast<object>().ToArray();
return ret;
}
}
This class is required by the dependency resolver:
public class WindsorDependencyScope : IDependencyScope
{
readonly List<object> _instances;
readonly Action<object> _release;
readonly IDependencyScope _scope;
public WindsorDependencyScope(IDependencyScope scope, Action<object> release)
{
if (scope == null)
{
throw new ArgumentNullException(nameof(scope));
}
if (release == null)
{
throw new ArgumentNullException(nameof(release));
}
_scope = scope;
_release = release;
_instances = new List<object>();
}
public void Dispose()
{
foreach (object instance in _instances)
{
_release(instance);
}
_instances.Clear();
}
public object GetService(Type t)
{
var service = _scope.GetService(t);
AddToScope(service);
return service;
}
public IEnumerable<object> GetServices(Type t)
{
var services = _scope.GetServices(t);
AddToScope(services);
return services;
}
void AddToScope(params object[] services)
{
if (services.Any())
{
_instances.AddRange(services);
}
}
}
And this is how you register the the dependency resolver in you Register method:
config.DependencyResolver = new WindsorDependencyResolver(container);
I would recommend to register your controllers with lifestyle transient as the dependency resolver takes care of disposing them.

How to use Dependency Injection with a Controller

I have below code which will work without any issue
MAUserController.cs
public class MAUserController : ApiController
{
ILogService loggerService;
IMAUserService _service;
public MAUserController(ILogService loggerService, IMAUserService Service)
{
this.loggerService = loggerService;
this._service = Service;
}
}
DependencyInstaller.cs
public class DependencyInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(
Component.For<ILogService>().ImplementedBy<LogService>().LifeStyle.PerWebRequest,
Component.For<IDatabaseFactory>().ImplementedBy<DatabaseFactory>().LifeStyle.PerWebRequest,
Component.For<IUnitOfWork>().ImplementedBy<UnitOfWork>().LifeStyle.PerWebRequest,
AllTypes.FromThisAssembly().BasedOn<IHttpController>().LifestyleTransient(),
AllTypes.FromAssemblyNamed("ISOS.Health.Service").Where(type => type.Name.EndsWith("Service")).WithServiceAllInterfaces().LifestylePerWebRequest(),
AllTypes.FromAssemblyNamed("ISOS.Health.Repository").Where(type => type.Name.EndsWith("Repository")).WithServiceAllInterfaces().LifestylePerWebRequest()
);
}
}
If I am using normal Controller instead ApiController then it gives me an error
UserController.cs
public class UserController : Controller
{
ILogService loggerService;
IMAUserService _service;
public UserController(ILogService loggerService, IMAUserService Service)
{
this.loggerService = loggerService;
this._service = Service;
}
}
This will give an error:
No parameterless constructor defined for this object
I am using CastleDI Windsor for Dependency injection.
Do I need to do anything or register something?
FIRST APPROACH
Advice: Use with caution, because it may cause memory leaks for Castle Windsor.
You have to create a controller activator, which should implement the IControllerActivator interface, in order to use your DI container to create the controller instances:
public class MyWindsorControllerActivator : IControllerActivator
{
public MyWindsorControllerActivator(IWindsorContainer container)
{
_container = container;
}
private IWindsorContainer _container;
public IController Create(RequestContext requestContext, Type controllerType)
{
return _container.Resolve(controllerType) as IController;
}
}
Then, add this class to your DependencyInstaller:
public class DependencyInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(
// Current code...
Component.For<IControllerActivator>()
.ImplementedBy<MyWindsorControllerActivator>()
.DependsOn(Dependency.OnValue("container", container))
.LifestyleSingleton();
);
}
}
Also, create your own dependency resolver based on the Windsor container:
public class MyWindsorDependencyResolver : IDependencyResolver
{
public MyWindsorDependencyResolver(IWindsorContainer container)
{
_container = container;
}
private IWindsorContainer _container;
public object GetService(Type serviceType)
{
return _container.Resolve(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return _container.ResolveAll(serviceType).Cast<object>();
}
}
Then, finally, register your dependency resolver in the Application_Start method in Global.asax.cs:
DependencyResolver.SetResolver(new MyWindsorDependencyResolver(windsorContainer));
This way, when MVC requires the controller activator through it's dependency resolver, it will get ours, which will use our Windsor container to create the controllers with all it's dependencies.
In order to avoid memory leaks using IControllerActivator, the easiest solution will be to use lifestyles like per thread or per web request, rather than the default (Singleton), transient and pooled, for the registered components. Check this link for more info about how to avoid memory leaks using Castle Windsor Container.
SECOND APPROACH
However, as pointed out by #PhilDegenhardt, a much better and correct approach will be to implement a custom controller factory, in order to be able to release the controller component created by the Castle Windsor DI Container. Here you can find an example (see the section about Dependency Injection).
Taken from that example, the implementation could be:
Global.asax.cs:
public class MvcApplication : System.Web.HttpApplication
{
private WindsorContainer _windsorContainer;
protected void Application_Start()
{
var _windsorContainer = new WindsorContainer();
_windsorContainer.Install(
new DependencyInstaller(),
// Other installers...
);
ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(_windsorContainer.Kernel));
}
protected void Application_End()
{
if (_windsorContainer != null)
{
_windsorContainer.Dispose();
}
}
}
WindsorControllerFactory.cs:
public class WindsorControllerFactory : DefaultControllerFactory
{
private readonly IKernel _kernel;
public WindsorControllerFactory(IKernel kernel)
{
_kernel = kernel;
}
public override void ReleaseController(IController controller)
{
_kernel.ReleaseComponent(controller); // The important part: release the component
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
{
throw new HttpException(404, string.Format("The controller for path '{0}' could not be found.", requestContext.HttpContext.Request.Path));
}
return (IController)_kernel.Resolve(controllerType);
}
}
Look at the following project link https://github.com/rarous/Castle.Windsor.Web.Mvc
Add this reference via NuGet to your MVC project, it will do the registering job for you.
Do not forget to catch your errors in global.asax.cs!
Registration :
container.Register(Component.For<IControllerFactory>().ImplementedBy<WindsorControllerFactory>());
Implementation of MVC controller factory :
using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Castle.MicroKernel;
namespace Installer.Mvc
{
public class WindsorControllerFactory : DefaultControllerFactory
{
private readonly IKernel _kernel;
public WindsorControllerFactory(IKernel kernel)
{
_kernel = kernel;
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
{
throw new HttpException(404, string.Format("The controller for path '{0}' could not be found.", requestContext.HttpContext.Request.Path));
}
if (_kernel.GetHandler(controllerType) != null)
{
return (IController)_kernel.Resolve(controllerType);
}
return base.GetControllerInstance(requestContext, controllerType);
}
public override void ReleaseController(IController controller)
{
_kernel.ReleaseComponent(controller);
}
}
}

Unable to perform dependency injection in MVC 5 Web API project using Castle Windsor

Below is the code for controller I want to instantiate using Windsor Castle.
public class TestController : ApiController
{
private ITestService _testService = null;
public TestController(ITestService testService)
{
_testService = testService;
}
public IList<TestClass> Get()
{
IList<TestClass> testObjects = _testService.GetAll().ToList();
return testObjects;
}
}
I've written following code in Global.asax.cs
protected void Application_Start()
{
........................
InitializeServiceLocator();
}
private static void InitializeServiceLocator()
{
_container = new WindsorContainer().Install(FromAssembly.This());
var controllerFactory = new WindsorControllerFactory(_container.Kernel);
ControllerBuilder.Current.SetControllerFactory(controllerFactory);
}
Here is the code for installer =>
public class ControllerInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
if (store == null)
{
throw new ArgumentNullException("store");
}
//All MVC controllers
container.Register(Classes.FromThisAssembly().BasedOn<IHttpController>().LifestylePerWebRequest());
AddComponentsTo(container);
}
private void AddComponentsTo(IWindsorContainer container)
{
container.Register(
///DBContext
Component.For<DbContext>().ImplementedBy<SCFEntities>().LifestyleTransient());
container.Register(
Classes.FromAssemblyNamed("MyProject.ApplicationServices").Pick().WithService.DefaultInterfaces().LifestylePerWebRequest(),
Classes.FromAssemblyNamed("MyProject.Data").Pick().WithService.DefaultInterfaces().LifestylePerWebRequest());
}
}
The problem is the controller instance is not created using parameterized constructor. It is expecting a parameterless constructor. Could anybody point out where I am going wrong? Thanks.
Be sure to read all the articles regarding WEB API that Mark Seemann wrote.
You can start here and then traverse the archive for Web API here.
Read the first article and then traverse the archive. Everything is here.

Setting up Inversion of Control (IoC) in ASP.NET MVC with Castle Windsor

I'm going over Sanderson's Pro ASP.NET MVC Framework and in Chapter 4 he discusses Creating a Custom Controller Factory and it seems that the original method, AddComponentLifeStyle or AddComponentWithLifeStyle, used to register controllers is deprecated now:
public class WindsorControllerFactory : DefaultControllerFactory
{
IWindsorContainer container;
public WindsorControllerFactory()
{
container = new WindsorContainer(new XmlInterpreter(new ConfigResource("castle")));
// register all the controller types as transient
var controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
where typeof(IController).IsAssignableFrom(t)
select t;
//[Obsolete("Use Register(Component.For<I>().ImplementedBy<T>().Named(key).Lifestyle.Is(lifestyle)) instead.")]
//IWindsorContainer AddComponentLifeStyle<I, T>(string key, LifestyleType lifestyle) where T : class;
foreach (Type t in controllerTypes)
{
container.Register(Component.For<IController>().ImplementedBy<???>().Named(t.FullName).LifeStyle.Is(LifestyleType.Transient));
}
}
// Constructs the controller instance needed to service each request
protected override IController GetControllerInstance(Type controllerType)
{
return (IController)container.Resolve(controllerType);
}
}
The new suggestion is to use Register(Component.For<I>().ImplementedBy<T>().Named(key).Lifestyle.Is(lifestyle)), but I can't figure out how to present the implementing controller type in the ImplementedBy<???>() method. I tried ImplementedBy<t>() and ImplementedBy<typeof(t)>(), but I can't find the appropriate way to pass in the implementing type. Any ideas?
I'm doing this using the ControllerBuilder.SetControllerFactory and the code you can find in the open source project MvcContrib:
Global.asax.cs
protected void Application_Start()
{
...
IWindsorContainer windsorContainer = new WindsorContainer();
windsorContainer.RegisterControllers(Assembly.GetExecutingAssembly());
ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(windsorContainer));
...
}
WindsorControllerFactory
public class WindsorControllerFactory : DefaultControllerFactory
{
private readonly IWindsorContainer _container;
public WindsorControllerFactory(IWindsorContainer container)
{
if (container == null)
{
throw new ArgumentNullException();
}
_container = container;
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
{
throw new HttpException();
}
if (!typeof(IController).IsAssignableFrom(controllerType))
{
throw new ArgumentException();
}
try
{
return (IController)_container.Resolve(controllerType);
}
catch (Exception ex)
{
throw new InvalidOperationException();
}
}
public override void ReleaseController(IController controller)
{
IDisposable disposable = controller as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
_container.Release(controller);
}
}
WindsorExtensions (see MvcContrib)
public static class WindsorExtensions
{
public static IWindsorContainer RegisterController<T>(this IWindsorContainer container) where T : IController
{
container.RegisterControllers(typeof(T));
return container;
}
public static IWindsorContainer RegisterControllers(this IWindsorContainer container, params Type[] controllerTypes)
{
foreach (Type type in controllerTypes)
{
if (ControllerExtensions.IsController(type))
{
container.Register(Component.For(type).Named(type.FullName).LifeStyle.Is(LifestyleType.Transient));
}
}
return container;
}
public static IWindsorContainer RegisterControllers(this IWindsorContainer container, params Assembly[] assemblies)
{
foreach (Assembly assembly in assemblies)
{
container.RegisterControllers(assembly.GetExportedTypes());
}
return container;
}
}
ControllerExtensions (see MvcContrib)
public static class ControllerExtensions
{
public static bool IsController(Type type)
{
return type != null
&& type.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase)
&& !type.IsAbstract
&& typeof(IController).IsAssignableFrom(type);
}
}
You may also want to consider using the new installer option in the latest Windsor build. There is more documentation on Windsor's tutorial: http://stw.castleproject.org/Windsor.Windsor-tutorial-part-three-writing-your-first-installer.ashx
There's a tutorial (in the works but 9 parts are already out) that discusses usage of Windsor in ASP.NET MVC here.
That's the most up to date and covering most of the usual usage resource on the topic as far as I'm aware.
#Lirik, as an addition: drop your own custom IControllerFactory out if you use MVC3. Just register controllers with Windsor and implement IDependencyResolver with Windsor container inside.
Set your IDependencyResolver as MVC DependencyResolver and DefaultControllerFactory will automatically wire up controllers registered in container (via DependencyResolver).
something like:
public void Register(IWindsorContainer container)
{
Assembly.GetAssembly(typeof(ControllersRegistrarMarker)).GetExportedTypes()
.Where(IsController)
.Each(type => container.AddComponentLifeStyle(
type.Name.ToLower(),
type,
LifestyleType.Transient));
}
ControllersRegistrarMarker is just an empty class in your Controllers assembly

Resources