Search code examples
asp.net-mvc-2dependency-injectioninversion-of-controlenterprise-libraryunity-container

.Net Unity Object instance issues


I really stuck trying to get unity to work for a c# project I'm working on.

Its throwing an "Object reference not set to an instance of an object" error in my unity controller factory class.

UnityControllerFactory.cs file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

namespace Sportrax.Presentation.Admin
{
    public class UnityControllerFactory :  DefaultControllerFactory
    {
        IUnityContainer _container;

        public UnityControllerFactory(IUnityContainer container)
        {
            _container = container;
        }

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            return (IController)_container.Resolve(controllerType);
        }

    }
}

My Global.asax.cs file is:

 protected void Application_Start()
        {
            IUnityContainer container = new UnityContainer();
            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");

            section.Containers.Default.Configure(container);

            UnityControllerFactory factory = new UnityControllerFactory(container);
            ControllerBuilder.Current.SetControllerFactory(factory);

            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);
        }

And my Web.config is:

<configuration>
    <configSections>
      <section name ="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
    </configSections>
    <unity>
        <containers>
            <container>
                <types>
                    <type type="Sportrax.Business.Interfaces.IVenueService, Sportrax.Business" mapTo="Sportrax.Business.VenueService, Sportrax.Business" />
                    <type type="Sportrax.DAL.Interfaces.IVenueRepository, Sportrax.DAL" mapTo="Sportrax.DAL.Repositories.VenueRepository, Sportrax.DAL" />
                </types>
            </container>
        </containers>
    </unity>
    ......

I have added project references to Microsoft.Practices.unity and Microsoft.Practices.Unity.Configuration, version 1.1.0.0.

My runtime version under those references properties says v2.0.50727

Could this be the issue? I do have two versions availble when I go to add the references?

Adding error details here (I cant attach images!):

System.NullReferenceException was unhandled by user code
  Message=Object reference not set to an instance of an object.
  Source=Microsoft.Practices.Unity
  StackTrace:
       at Microsoft.Practices.Unity.ResolutionFailedException.CreateMessage(Type typeRequested, String nameRequested, Exception innerException) in e:\Builds\Unity\UnityTemp\Compile\Unity\Src\Unity\ResolutionFailedException.cs:line 99
       at Microsoft.Practices.Unity.ResolutionFailedException..ctor(Type typeRequested, String nameRequested, Exception innerException) in e:\Builds\Unity\UnityTemp\Compile\Unity\Src\Unity\ResolutionFailedException.cs:line 43
       at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name) in e:\Builds\Unity\UnityTemp\Compile\Unity\Src\Unity\UnityContainer.cs:line 467
       at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, String name) in e:\Builds\Unity\UnityTemp\Compile\Unity\Src\Unity\UnityContainer.cs:line 450
       at Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name) in e:\Builds\Unity\UnityTemp\Compile\Unity\Src\Unity\UnityContainer.cs:line 149
       at Microsoft.Practices.Unity.UnityContainerBase.Resolve(Type t) in e:\Builds\Unity\UnityTemp\Compile\Unity\Src\Unity\UnityContainerBase.cs:line 416
       at Sportrax.Presentation.Admin.UnityControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) in C:\Users\Thomas\Documents\College\MSc Web Technologies\Project .Net\Sportrax\Sportrax.Solution\Sportrax.Presentation.Admin\UnityControllerFactory.cs:line 23
       at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
       at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
       at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
       at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state)
       at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
       at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
       at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
  InnerException: 

It occurs at line:

return (IController)_container.Resolve(controllerType);

in UnityControllerFactory.cs


Solution

  • I'm starting to think this is a dll instalation/config problem. It's a team project and works fine on others machines. I have deleted my projects and re-checked in a clean version into my machine.

    I keep getting VisualStudio File pop-up dialog boxes when its hits the error on line 'return (IController)_container.Resolve(controllerType);' in the factory class.

    The File pop up is asking to find source files: So far files asked for are: e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainerExtensions.cs e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\Utility\Guard.cs e:\Builds\Unity\UnityTemp\Compile\Unity\Src\Unity.Configuration\UnityTypeResolver.cs e:\Builds\Unity\UnityTemp\Compile\Unity\Src\Unity\ResolutionFailedException.cs"

    I noticed that under 'C:\Program Files\' I have both Microsoft Unity Application Block 1.1 and Microsoft Unity Application Block 2.0 folders.

    Also, I'm on Vista OS....maybe something quirky with this?