简体   繁体   English

具有WebForms公共属性的Autofac引发构造函数异常

[英]Autofac with WebForms public property throws constructor exception

I am trying to modify an existing WebForms application and would like to use Autofac. 我正在尝试修改现有的WebForms应用程序,并想使用Autofac。 As far as I can tell, I've set up the application according to the documentation ( http://autofac.readthedocs.org/en/latest/integration/webforms.html#structuring-pages-and-user-controls-for-di ). 据我所知,我已经根据文档( http://autofac.readthedocs.org/en/latest/integration/webforms.html#structuring-pages-and-user-controls-for -di )。

Web page code with a public property 具有公共属性的网页代码

public partial class MyTestPage : Page
{
    public ILogger Logger { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        //page load code here

web.config 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 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());

    }

When I try to load the page, Autofac throws this exception. 当我尝试加载页面时,Autofac会引发此异常。

No constructors on type 'NLog.Logger' can be found with the constructor finder 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'. 使用构造函数查找器“ Autofac.Core.Activators.Reflection.DefaultConstructorFinder”找不到“ NLog.Logger”类型的构造函数。

What am I missing here? 我在这里想念什么? Why is Autofac looking for a constructor instead of picking up the public property? Autofac为什么要寻找构造函数而不是选择公共属性?

The problem here is that Autofac is trying to create an instance of Logger class to inject it to your MyTestPage . 这里的问题是Autofac试图创建Logger类的实例以将其注入到MyTestPage It tries using default, parameter-less constructor, which is unavailable. 它尝试使用默认的无参数构造函数,该构造函数不可用。 So the problem is not with your page, but with your logger registration. 因此,问题不在于您的页面,而在于您的记录器注册。

I am not familiar to NLog , but according to tutorial, you are suppose to use LogManager to create instances of Logger , eg 我对NLog不熟悉,但是根据教程,您假设使用LogManager创建Logger实例,例如

Logger logger = LogManager.GetCurrentClassLogger();

or 要么

Logger logger = LogManager.GetLogger("MyClassName");

So you should change your registration to something like this (not tested): 因此,您应该将注册更改为以下内容(未经测试):

builder.Register(c => LogManager.GetCurrentClassLogger()).As<ILogger>().InstancePerRequest();

As I mentioned above, I am not familiar with NLog, but I assume NLog Logger class implements ILogger interface. 如上所述,我对NLog不熟悉,但是我假设NLog Logger类实现ILogger接口。

EDIT: 编辑:

This module should handle what you are trying to achieve. 模块应处理您要实现的目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用VB.NET将Webforms Autofac参数传递给构造函数 - Webforms Autofac parameter to constructor using VB.NET 洋葱架构中的Autofac WebForms - Autofac WebForms in Onion Architecture Autofac - 确保控制器具有无参数的公共构造函数 - Autofac - Make sure that the controller has a parameterless public constructor ASP.NET WebForms和SimpleInjector FindControl不会抛出无参数构造函数 - ASP.NET WebForms and SimpleInjector FindControl throws no parameterless constructor 在Autofac“属性注入”/ ASP.NET Webforms应用程序中解析相同类型的2个属性 - Resolve 2 properties of the same type in Autofac “property injection” / ASP.NET Webforms Application Autofac异常:无法解析构造函数&#39;Void .ctor的参数 - Autofac Exception: Cannot resolve parameter of constructor 'Void .ctor 使用autofac,webforms和ServiceLocator的生命周期范围 - Lifetime scope with autofac, webforms and ServiceLocator StructureMap认为它必须注入构造函数并引发异常 - StructureMap thinks it has to inject to constructor and throws exception Autofac Web API 错误确保控制器具有无参数的公共构造函数 - Autofac Web API error Make sure that the controller has a parameterless public constructor Autofac Webforms 可以忽略回发依赖注入 - Autofac Webforms is there away to ignore postback dependency injection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM