我面临着依赖注入(简单注入器)的问题,但它似乎只发生在将应用程序部署到运行IIS的服务器上,并且只有在它运行一段时间后才会发生 . 基于下面的StackTrace,似乎在依赖注入期间发生了NullReference异常 .

The interesting part is that if I restart the IIS Application, the application seems to continue working for some time. At a later point, presuming that no one has accessed the website, the following StackTrace is displayed:

[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(Container container)298 BranchCircuits_Web.MvcApplication.Application_Start()299 [HttpException(0x80004005):对象引用未设置为对象的实例 . ]系统.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context,HttpApplication app)12601949 System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext,HttpContext context,MethodInfo [] handlers)175 System.Web.HttpApplication.InitSpecial(HttpApplicationState state,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):对象引用未设置为对象的实例 . ]系统 . Web.HttpRuntime.FirstRequestInit(HttpContext context)12618996 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context)159 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr,HttpContext context)12458613

Below is the Global.asax contents that is in charge for the dependency injection.

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时遇到过这种情况?根本原因是什么,如何解决?