我正在研究mvc-5应用程序,它也有webapi。应用程序正在抛出System.NullReference异常。
错误消息
System.NullReferenceException: Object reference not set to an instance of an object.
Stack trace
at Unity.Mvc5.UnityDependencyResolver.DisposeOfChildContainer()
at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
这是容器的构建方式,并在全局中从Application_Start调用,作为UnityBootstrapper.Initialize(); -
public static class UnityBootstrapper
{
public static void Initialize()
{
var container = BuildUnityContainer();
DependencyResolver.SetResolver(new Unity.Mvc5.UnityDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
ControllerBuilder.Current.SetControllerFactory(new UnityMvcControllerFactory(container));
}
private static IUnityContainer BuildUnityContainer()
{
var container = new UnityContainer();
// MVC/WebApi controller factories
container.RegisterInstance<IHttpControllerActivator>(new UnityApiControllerActivator(container,
new DefaultHttpControllerActivator()));
// NHibernate sessions
container.RegisterInstance<ISessionBuilder>(SessionBuilder.Instance);
container.RegisterType<IClientCodeRequestContext, ClientCodeRequestContext>(
new CurrentRequestContextLifetimeManager<IClientCodeRequestContext>());
container.RegisterType<IUnitOfWork, UnitOfWork>(
new CurrentRequestContextLifetimeManager<IUnitOfWork>(),
new InjectionConstructor(container.Resolve<ISessionBuilder>()));
container.RegisterType<IUnitOfWorkContext>(
new InjectionFactory(c => container.Resolve<IUnitOfWork>().CommonContext));
container.RegisterType<IUnitOfWorkClientContext>(
new InjectionFactory(
c => container.Resolve<IUnitOfWork>()[container.Resolve<IClientCodeRequestContext>().ClientCode]));
//Repositories
container.RegisterType<IUserRepository, UserRepository>();
//Business Logic
container.RegisterType<IMembershipPrincipalBusiness, MembershipPrincipalBusiness>();
// Other
container.RegisterType<IAuthenticationProvider, CoreMembershipProvider>();
return container;
}
}
在global.asax.cs -
中protected void Application_PostAuthenticateRequest(object sender, EventArgs e)
{
var principalBusiness = DependencyResolver.Current.GetService<IMembershipPrincipalBusiness>();
}
这个问题仅在生产中,并且被新生物捕获。
异常是一个统一的错误还是在这个实现中错过了什么。我一周以来一直在搜索这个问题,但却找不到任何东西。