我遇到了依赖注入(Simple Injector)的问题,但是只有当应用程序部署到运行IIS的服务器时才会出现,并且只有在它运行一段时间后才会出现。基于下面的StackTrace,似乎在依赖注入期间发生了NullReference异常。
有趣的是,如果我重新启动IIS应用程序,应用程序似乎会继续工作一段时间。稍后,假设没有人访问过该网站,将显示以下StackTrace:
[NullReferenceException:对象引用未设置为的实例 对象。]
DevOps.Data.DevOpsDbConnectionLoader.GetObjectInstanceInternal(大会 dbAssembly,String objectTypeName,Boolean isStatic)+16
DevOps.Data.DevOpsDbConnectionLoader.GetEntityDbProviderServices() +156 NWatch.NWatchDbConfiguration..ctor(DevOpsDbConnectionLoader loader)+239 NWatch.NWatchEntityApplication.Init()+ 429
BranchCircuits_Web.MvcApplication.RegisterTypes(容器容器) +298 BranchCircuits_Web.MvcApplication.Application_Start()+299[HttpException(0x80004005):未将对象引用设置为实例 一个对象。] System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext的 上下文,HttpApplication app)+12601949
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr的 appContext,HttpContext上下文,MethodInfo []处理程序)+175
System.Web.HttpApplication.InitSpecial(HttpApplicationState状态, MethodInfo [] handlers,IntPtr appContext,HttpContext context)+304
System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr的 appContext,HttpContext context)+404
System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr的 appContext)+475[HttpException(0x80004005):未将对象引用设置为实例 一个对象。] System.Web.HttpRuntime.FirstRequestInit(HttpContext 上下文)+12618996
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +159 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest) wr,HttpContext context)+12458613
下面是负责依赖注入的Global.asax内容。
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
var webApiContainer = new Container();
webApiContainer.Options.DefaultScopedLifestyle = new WebApiRequestLifestyle();
RegisterTypes(webApiContainer);
webApiContainer.RegisterWebApiControllers(GlobalConfiguration.Configuration);
webApiContainer.Verify();
GlobalConfiguration.Configuration.DependencyResolver =
new SimpleInjectorWebApiDependencyResolver(webApiContainer);
}
private void RegisterTypes(Container container)
{
var virtualPath = HostingEnvironment.ApplicationVirtualPath.Substring(1);
string baseName = null;
if (!string.IsNullOrEmpty(virtualPath)) {
baseName = HostingEnvironment.SiteName + "_" + virtualPath;
} else {
baseName = HostingEnvironment.SiteName;
}
var nWatchApp = new NWatchEntityApplication(GetNWatchConfig());
Trace.Listeners.Add(new DevOps.Diagnostics.DevOpsLogTraceListener(baseName));
container.RegisterSingleton<INWatchApplication>(nWatchApp);
container.Register<NWatchDbContext>(() => nWatchApp.GetDbContext(), Lifestyle.Scoped);
}
private INWatchConfiguration GetNWatchConfig()
{
Configuration rootConfig =
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HostingEnvironment.ApplicationVirtualPath);
return new NWatchSystemConfiguration(rootConfig);
}
在部署到IIS时有没有人遇到过这种情况?根本原因是什么,如何解决?