Does/Will autofac's ASP.NET integration support PreInit or Init events?
Posted
by David Rubin
on Stack Overflow
See other posts from Stack Overflow
or by David Rubin
Published on 2010-03-10T23:45:39Z
Indexed on
2010/03/15
17:49 UTC
Read the original article
Hit count: 257
I see from poking around in the 1.4.4 source that Autofac's ASP.NET integration (via Autofac.Integration.Web) peforms injection of properties on the Page
as part of the HttpContext.PreRequestHandlerExecute
event handling, but that the page's child controls don't get their properties injected until Page.PreLoad
.
What this means, though is that the injected properties of child controls are unavailable for use in the OnInit event handler.
For example, this works fine:
HelloWorld.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="HelloWorld.aspx.cs" Inherits="HelloWorld" %>
<html>
<body>
<asp:Label runat="server" id="lblMsg" OnInit="HandleInit"/>
</body>
</html>
HelloWorld.aspx.cs:
...
protected void HandleInit()
{
lblMsg.Text = _msgProvider.GetMessage();
}
public IMsgProvider _msgProvider { private get; set; } // <-- Injected
But changing the HelloWorld Page
to a UserControl
(.acsx) and putting the UserControl in another page doesn't work because _msgProvider
isn't injected early enough.
Is there a way to make Autofac inject properties of child controls earlier? Or is this something that can be addressed in a future build? Thanks!
© Stack Overflow or respective owner