Search code examples
c#asp.net-mvc-3dependency-injectionunity-containerunity2.0

Unity 2 is behaving strangely with generics


I am right now having big trouble with Unity 2 in my MVC 3 project.

I have created an abstract BaseViewPage that all viewpages inherits from. In this i have 2 dependencies. So far so good. Now i have 3 different Viewpages that inherit from the BaseViewPage. These uses generics to deliver some specific data to the view. So far so good. Now comes the problem. In my homecontroller i use unity to resolve one of those viewpages. The viewpage gets loaded correctly when i debug it but right after my call to return view("index", model); unity makes a call to one of those dependencies inside the baseviewpage. This is done when the httpcontext is null.

Unity config (Loads all the viewpages):

container.RegisterType<IBackendWrapper, BackendWrapper.BackendWrapper>(new PerThreadLifetimeManager());
            container.RegisterType<BaseViewPage, EmptyViewPage>("EmptyViewPage");
            container.RegisterType(typeof(BaseViewPage), typeof(GenericViewPage<>), "GenericViewPage");
            container.RegisterType(typeof(BaseViewPage), typeof(GenericIEnumerableViewPage<>), "GenericIEnumerableViewPage");

BaseViewPage and one generic view page (the other generic pages looks almost the same):

public abstract class BaseViewPage 
{
        [Dependency]
        public IBackendWrapper Backend { get; set; }
        ....
}

public class GenericViewPage<T> : BaseViewPage
{
    public T Model { get; set; }

    public GenericViewPage(T model)
        : base()
    {
        Model = model;
    }
}

Now in my home controller i have first a dependency to Backend (to test that it works), then inside the Index i use unity to resolve one generic view page:

public class HomeController : Controller
{
    [Dependency]
    public IBackendWrapper Backend { get; set; }

    public ActionResult Index(MvcLoginUser user)
    {
        var model = UnityGlobalContainer.Container.Resolve<GenericViewPage<MvcLoginUser>>("GenericViewPage");
        return View("Index", model);
    }
 }

Now after the return, unity makes a call to the BackendWrapper object. More precisely BackendWrapper.UserIdent.TheLogin. TheLogin throws an error since there is no HttpContext present. Question is, why does unity try to access it? I have implemented a dispose inside the BackendWrapper and inside the UserIdent but unity ignores them and still calls TheLogin.

Inside the BackendWrapper i have a constructor that calls an external dll to create a new UserIdent. So its nothing that Unity is resolving. But unity still tries to access it. Also, if i remove the dependency from the BaseViewPage it works perfectly. So only when BaseViewPage has a dependency to BackendWrapper is this problem occurring. Did i configure it correctly?


Solution

  • I found out that this "Application_EndRequest" was hit BEFORE unity has cleaned up its objects. This in turn terminated our NHibernate session, so when unity then tried to dispose of all objects inside the created classes it would fail. Still have not figured out why it tries to dispose of non created classes. The wrapperclass was created by unity and the wrapper class in turn created the UserIdent. My guess is that the garbage collector or something ran and tried to go through all the created classes and their child objects. This was done outside the HttpContext which resulted in several exceptions.