I want to inject an interface into an asp user control. My classes look like:
public partial class PercentByState : CommonControlsData
{
[Dependency]
public IChartService ChartService { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = ReportService.DoSomething();
}
}
}
public class CommonControlsData : System.Web.UI.UserControl
{
[Dependency]
public IReportService ReportService { get; set; }
}
I use Unity and Unity.WebForms nuget packages.
RegisterDependencies method looks like:
private static void RegisterDependencies( IUnityContainer container )
{
//services
container.RegisterType<IReportService, ReportService>();
}
ReportService and ChartService are null in Page_Load() function.
Any ideas what am I doing wrong ?
Edit: I am dynamically loading the controls with the following code:
TableRow tableRow = new TableRow();
TableCell tableCell = new TableCell();
string controlName = feature.ControlName;
tableCell.Controls.Add(Page.LoadControl(controlName));
tableRow.Controls.Add(tableCell);
MainContentTable.Rows.Add(tableRow);
Also, my controls are not registered to the page with a Register command. I think it has something to do with this type of loading.
Dependencyare what is injected. If the UserControls are not Page dependencies then I don't think they will be "Built Up" by Unity.