【发布时间】:2016-03-15 01:53:38
【问题描述】:
我正在尝试修改现有的 WebForms 应用程序并希望使用 Autofac。据我所知,我已经根据文档 (http://autofac.readthedocs.org/en/latest/integration/webforms.html#structuring-pages-and-user-controls-for-di) 设置了应用程序。
具有公共属性的网页代码
public partial class MyTestPage : Page
{
public ILogger Logger { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
//page load code here
web.config
<modules>
<add name="ContainerDisposal" type="Autofac.Integration.Web.ContainerDisposalModule, Autofac.Integration.Web" preCondition="managedHandler" />
<add name="PropertyInjection" type="Autofac.Integration.Web.Forms.PropertyInjectionModule, Autofac.Integration.Web" preCondition="managedHandler" />
</modules>
Global.asax.cs
public class Global : HttpApplication, IContainerProviderAccessor
{
static IContainerProvider _containerProvider;
public IContainerProvider ContainerProvider
{
get { return _containerProvider; }
}
void Application_Start(object sender, EventArgs e)
{
var builder = new ContainerBuilder();
builder.RegisterType<Logger>().As<ILogger>().InstancePerRequest();
_containerProvider = new ContainerProvider(builder.Build());
}
当我尝试加载页面时,Autofac 抛出此异常。
无法使用构造函数查找器“Autofac.Core.Activators.Reflection.DefaultConstructorFinder”找到类型为“NLog.Logger”的构造函数。
我在这里缺少什么?为什么 Autofac 寻找构造函数而不是获取公共属性?
【问题讨论】: