• IContainerProviderAccessor.ContainerProvider returned null


    IContainerProviderAccessor.ContainerProvider returned null, which is invalid. If the container provider belongs to the HttpApplication subclass, ensure that it is a static variable.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidOperationException: IContainerProviderAccessor.ContainerProvider returned null, which is invalid. If the container provider belongs to the HttpApplication subclass, ensure that it is a static variable.

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    在webforms里面遇到这个错误,

    排查之后,发现没有按照标准模式写代码

    https://autofaccn.readthedocs.io/en/latest/integration/webforms.html

    public class Global : HttpApplication, IContainerProviderAccessor
    {
      // Provider that holds the application container.
      static IContainerProvider _containerProvider;
    
      // Instance property that will be used by Autofac HttpModules
      // to resolve and inject dependencies.
      public IContainerProvider ContainerProvider
      {
        get { return _containerProvider; }
      }
    
      protected void Application_Start(object sender, EventArgs e)
      {
        // Build up your application container and register your dependencies.
        var builder = new ContainerBuilder();
        builder.RegisterType<SomeDependency>();
        // ... continue registering dependencies...
    
        // Once you're done registering things, set the container
        // provider up with your registrations.
        _containerProvider = new ContainerProvider(builder.Build());
      }
    }

    标准写法里面的注入代码是写在Application_Start方法中的。

    但是错误的代码是,写了一个静态构造函数,在静态构造函数里面执行了注入操作。

    static Global()
    {
      // Build up your application container and register your dependencies.
        var builder = new ContainerBuilder();
        builder.RegisterType<SomeDependency>();
        // ... continue registering dependencies...
    
        // Once you're done registering things, set the container
        // provider up with your registrations.
        _containerProvider = new ContainerProvider(builder.Build());
    }

    这样直接屏蔽掉了正确错误

    解决方案就是按照官方的处理,在Application_Start方法中添加注入代码。这样会得到正确的错误,

    The application could not connect to the database, please check the connection string in the web.config file and SQL server availability.

    Original error:
    A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

    kentico原生的Global.asax.cs

    using System;
    using System.Reflection;
    using CMS.Base;
    using CMS.Core;
    using CMS.DataEngine;
    
    /// <summary>
    /// Application methods.
    /// </summary>
    public class Global : CMSHttpApplication
    {
        #region "Methods"
    
        static Global()
        {
    #if DEBUG
            // Set debug mode based on current web project build configuration
            SystemContext.IsWebProjectDebug = true;
    #endif
    
            // Ensure initialization of dynamic modules after application pre-initialization. Must be called before the StartApplication method.
            ApplicationEvents.PreInitialized.Execute += EnsureDynamicModules;
    
            // Initialize CMS application. This method should not be called from custom code.
            InitApplication();
        }
    
    
        /// <summary>
        /// Ensures that modules from the App code (for web site) and CMSApp (for web application) assembly are registered.
        /// </summary>
        private static void EnsureDynamicModules(object sender, EventArgs e)
        {
            // Global.asax.cs is hosted in AppCode/CMSApp assembly
            var appCodeAssembly = Assembly.GetExecutingAssembly();
    
            // Ensures that registered modules are registered from AppCode/CMSApp assembly
            ModuleEntryManager.EnsureAppCodeModules(appCodeAssembly);
        }
    
        #endregion
    }
  • 相关阅读:
    java中如何高效的判断数组中是否包含某个元素---
    反射--
    Json----
    Ajax学习(1)
    Jdbc学习---
    java---内部类
    java中的多态
    spring是什么
    quartz的配置文件
    浅谈Job&JobDetail
  • 原文地址:https://www.cnblogs.com/chucklu/p/12699321.html
Copyright © 2020-2023  润新知