【问题标题】:AutoFac System.InvalidOperationException: Server could not create ASP.frmpaymentadd_aspxAutoFac System.InvalidOperationException:服务器无法创建 ASP.frmpaymentadd_aspx
【发布时间】:2022-01-15 00:38:52
【问题描述】:

我正在研究一个相对脆弱的遗留解决方案,我希望引入 AutoFac,以便我可以引入单元测试并注入各种东西,例如 Serilog.ILogger。

这是我的 web.config 中的内容

<system.webServer>
    <modules>
        <!-- This module handles disposal of the request lifetime scope. -->
        <add name="ContainerDisposal" type="Autofac.Integration.Web.ContainerDisposalModule, Autofac.Integration.Web" preCondition="managedHandler" />
        <!-- This module injects properties on web forms. You could also use the UnsetPropertyInjectionModule or a custom module. -->
        <add name="PropertyInjection" type="Autofac.Integration.Web.Forms.PropertyInjectionModule, Autofac.Integration.Web" preCondition="managedHandler" />
    </modules>
</system.webServer>

我正在按照下面的代码块在 Global.ascx.cs 中注册 AutoFac

private void AutofacRegister()
{
    var builder = new ContainerBuilder();

           
    builder.Register<Serilog.ILogger>((c, p) =>
    {
        return new LoggerConfiguration()
            .ReadFrom.AppSettings()
            .Enrich.WithMachineName()
            .CreateLogger();
    }).SingleInstance();

    //Set Dependent Parser
    _containerProvider = new ContainerProvider(builder.Build());
}

我正在尝试将基本日志记录引入其中一个页面

public partial class frmPaymentAdd : _SmartPayPage
{
    private readonly ILogger _Logger;

    public frmPaymentAdd(ILogger logger)
    {
        _Logger = logger;
    }
    
    .....
}

一切都编译并运行,但是当我导航到相应的页面时,我收到以下错误

在 __ASP.FastObjectFactory_app_web_usuz0hix.Create_ASP_frmpaymentadd_aspx() 在 System.Web.Compilation.BuildResultCompiledType.CreateInstance() 在 System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath,类型 requiredBaseType,HttpContext 上下文,布尔 allowCrossApp) 在 System.Web.UI.PageHandlerFactory.GetHandlerHelper(HttpContext 上下文,字符串 requestType,VirtualPath virtualPath,字符串物理路径) 在 System.Web.UI.PageHandlerFactory.GetHandler(HttpContext 上下文,字符串 requestType,字符串 virtualPath,字符串路径) 在 System.Web.HttpApplication.MaterializeHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 在 System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep 步骤) 在 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

如果有人能帮助我指出我需要做的事情的正确方向,或者我错过了什么才能让这个运行起来,我将不胜感激。

谢谢

西蒙

【问题讨论】:

    标签: c# asp.net webforms autofac .net-4.7.2


    【解决方案1】:

    用于 Web 表单的 Autofac 仅注入属性,而不注入构造函数。使表单上的构造函数采用零参数并使记录器成为获取/设置属性。 There is a working example here.

    除此之外:我没有看到您发布了您实现IContainerProviderAccessor 的代码的 Global.asax 部分。我假设您已经完成了该部分,因为您在显示的代码中设置了_containerProvider,但是由于您实际上没有显示其余部分,所以我不能确定。确保你也有那部分,否则属性注入将不起作用。

    【讨论】:

    • 我看过那个工作示例,我从那个工作示例创建了一个全新的应用程序,甚至使用了那个工作示例并在遵循它时使用。我得到一个对象不是对象的实例错误。
    • 如果您遵循示例 - Web 表单上的无参数构造函数,获取/设置 Web 表单上的属性以进行注入,IContainerProviderAccessor - 那么问题可能不在 Autofac 集成中,而在其他地方.您发布的异常有堆栈跟踪,但没有实际的 message 所以这里没有其他任何人可以继续提供帮助。对不起。
    • 好吧,我猜消息在问题的标题中 - InvalidOperationException - 但听起来你现在遇到了一个新的/不同的错误,所以......这里还不够继续.
    猜你喜欢
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    • 2014-06-04
    • 2014-09-16
    • 1970-01-01
    • 2018-07-27
    • 2016-05-17
    • 1970-01-01
    相关资源
    最近更新 更多