.Net Unity 对象实例问题

发布于 2024-10-06 08:37:27 字数 5101 浏览 4 评论 0原文

我真的一直在努力让 Unity 为我正在从事的 ac# 项目工作。

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

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中

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少女净妖师 2024-10-13 08:37:27

它似乎是在 Microsoft.Practices.Unity.UnityContainer.DoBuildUp 引起的。我大胆猜测它可能是您类型之一的构造函数中的某些内容?

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?

硬不硬你别怂 2024-10-13 08:37:27

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

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

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.

冷情 2024-10-13 08:37:27

我开始认为这是 dll 安装/配置问题。这是一个团队项目,在其他机器上运行良好。我已删除我的项目并重新将干净版本签入我的计算机。

当 VisualStudio 文件遇到“return (IController)_container.Resolve(controllerType);”行错误时,我不断收到 VisualStudio 文件弹出对话框在工厂类中。

文件弹出窗口要求查找源文件:到目前为止,要求的文件有:
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"

我注意到在“C:\Program Files\”下我同时拥有 Microsoft Unity Application Block 1.1 和 Microsoft Unity Application Block 2.0 文件夹。

另外,我使用的是 Vista 操作系统......也许这有什么奇怪的?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文