[英]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.