.Net Unity Object实例问题

时间:2010-12-03 14:39:52

标签: asp.net-mvc-2 dependency-injection inversion-of-control enterprise-library unity-container

我真的不想让团结为我正在研究的c#项目工作。

它在我的统一控制器工厂类中抛出了“对象引用没有设置为对象的实例”错误。

UnityControllerFactory.cs文件:

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);
        }

    }
}

我的Global.asax.cs文件是:

 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);
        }

我的Web.config是:

<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>
    ......

我已将项目引用添加到Microsoft.Practices.unity和 Microsoft.Practices.Unity.Configuration,版本1.1.0.0。

我在这些引用属性下的运行时版本说v2.0.50727

这可能是问题吗?当我去添加引用时,我有两个版本可用吗?

在此处添加错误详细信息(我无法附加图片!):

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: 

它出现在以下行:

return(IController)_container.Resolve(controllerType);

UnityControllerFactory.cs中的

3 个答案:

答案 0 :(得分:0)

你偶然发现了Unity中的一个错误,这是肯定的。一个可重用的框架永远不应该抛出NullReferenceException。您可能配置错误,但通过查看异常无法确定原因是什么。

当我使用Reflector查看Unity v2.0的ResolutionFailedException.CreateMessage方法时,我找不到任何可以抛出NullReferenceException的点。因此,您必须使用旧版本,我猜他们在永不版本中修复了错误。尝试使用v2.0。我敢打赌,在这种情况下你得到一个更好的异常消息。

答案 1 :(得分:0)

它似乎是在Microsoft.Practices.Unity.UnityContainer.DoBuildUp引起的。我猜想它可能是你某个类型的构造函数中的东西吗?

答案 2 :(得分:0)

我开始认为这是一个dll instalation / config问题。这是一个团队项目,在其他机器上运行良好。我删除了我的项目,并在一个干净的版本中重新检查了我的机器。

当它遇到“return(IController)_container.Resolve(controllerType);”行上的错误时,我一直收到VisualStudio文件弹出对话框。在工厂上课。

文件弹出窗口要求查找源文件:到目前为止要求的文件是: E:\构建\团结\ UnityTemp \编译\团结\统一的\ src \ UnityContainerExtensions.cs E:\构建\团结\ UnityTemp \编译\团结\统一的\ src \工具\ Guard.cs E:\构建\团结\ UnityTemp \编译\统一的\ src \ Unity.Configuration \ UnityTypeResolver.cs E:\构建\统一\ UnityTemp \编译\统一\ SRC \统一\ ResolutionFailedException.cs“

我注意到在'C:\ Program Files \'下我有Microsoft Unity Application Block 1.1和Microsoft Unity Application Block 2.0文件夹。

另外,我在使用Vista操作系统......可能有些奇怪吗?