IIS池回收上的ASP.NET API DI(简单注入器)空引用 [英] ASP.NET API DI (Simple Injector) null reference on IIS pool recycle

查看:62
本文介绍了IIS池回收上的ASP.NET API DI(简单注入器)空引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前发布了另一个问题,但是经过观察,我已经缩小到可能导致我的问题的原因了。基本上,一旦IIS应用程序池被回收,我的依赖项注入(最终将通过创建NWatchApplication来扫描某些DLL)失败。 INWatchApplication是Web API项目的依赖项。

I previously posted another question, but after some observations, I have narrowed down to what it may be that's causing my problem. Basically, once the IIS Application Pool is recycled, my dependency injection (which ends up scanning for some DLLs through creating an NWatchApplication), fails. INWatchApplication is a dependency for the Web API project.

以下是Global.asax的内容:

Here are the contents of the 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);
}

我读过许多博客和文章,谈到动态加载的DLL似乎是如何这是一个问题,因为当IIS回收池时,它们不会被复制到AppDomain中。 (在这里我可能完全错了,但这是我的怀疑)。

I have read various blogs and posts talking about how dynamically loaded DLLs seem to be a problem, because when IIS recycles the pools, they aren't copied to the AppDomain. (I could be totally wrong here, but it is my suspicion).

如何确保所有可能的DLL都已加载(当应用程序首次部署到IIS时) ),即使在回收后仍可继续用于该应用程序?

How do I ensure that all DLLs that may have loaded (when the Application was first deployed to IIS) continue to be available to the Application even after a recycle?

推荐答案

var assemblies = BuildManager.GetReferencedAssemblies().Cast<Assembly>();

将其放入Global.asax Application_Start()内似乎已解决了该问题!

Put inside of the Global.asax Application_Start() seems to have resolved the issue!

这篇关于IIS池回收上的ASP.NET API DI(简单注入器)空引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆