我真的堅持嘗試讓我工作的c#項目的團結工作。.Net Unity對象實例問題
它在我的統一控制器工廠類中拋出「未設置爲對象實例的對象引用」錯誤。
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:
它發生在行:
回報(一個IController)_container.Resolve(controllerType);
在UnityControllerFactory.cs
這個東西已經變得不可能遵循了,SO不是一個論壇,答案會在頁面上向上或向下移動,所以說「看到頁面結尾」不起作用。如果你仍然沒有得到答案,你能編輯你的原始問題以包括你的現狀嗎? – 2010-12-09 21:31:45
同意的克里斯,它到處都是......抱歉,只有新來的,並不是100%確定如何發佈工作,認爲我現在會關閉問題把我的東西存入存儲庫,並在別人的結帳副本上統一測試。由於項目不久就要交付,不能再浪費時間來解決這個問題了。如果我有時間查看並在稍後的日期修復,我會更新答案。 – 2010-12-12 03:45:18