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
}
复制代码

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(733)  评论(3编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示