【发布时间】: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