.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 GetControl开发者_如何学PythonlerInstance(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
It seems to be causing it at Microsoft.Practices.Unity.UnityContainer.DoBuildUp
. I would hazard a guess that it could be something in the constructor of one of your types?
You stumbled upon a bug in Unity, that's for sure. A reusable framework should never throw a NullReferenceException
. You probably misconfigured it, but it is impossible to tell what the cause of this is by looking at the exception.
When I look at the ResolutionFailedException.CreateMessage
method of Unity v2.0 using Reflector, I can't find any spot that could throw an NullReferenceException
. Therefore you must be using an older version and I guess they fixed the error in a never version. Try using v2.0. I bet you get a better exception message in that case.
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?
精彩评论