0

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.

4
  • Typically, for ASP.NET WebForms Page properties marked as a Dependency are what is injected. If the UserControls are not Page dependencies then I don't think they will be "Built Up" by Unity. Commented Jul 20, 2017 at 16:00
  • how can I inject in User Controls then ? Commented Jul 20, 2017 at 19:37
  • I think I was wrong...If you're using Unity.WebForms then it looks like all controls should be built up (and dependencies injected). Hard to say from what you've posted what the issue is -- posting a Minimal, Complete, and Verifiable example would help (although there are a lot of files involved). Other things to consider: are any dependencies being injected or is it just an issue with child controls, is the UnityHttpModule registered? Commented Jul 20, 2017 at 21:56
  • @Randy Levy : I posted an edit. It might shed some light. Commented Jul 22, 2017 at 13:57

1 Answer 1

1

Since you are dynamically loading controls you'll need to build them up manually:

TableRow tableRow = new TableRow();
TableCell tableCell = new TableCell();
string controlName = feature.ControlName;
var control = Page.LoadControl(controlName);
Context.GetChildContainer().BuildUp(control.GetType().UnderlyingSystemType, control);
tableCell.Controls.Add(control);
tableRow.Controls.Add(tableCell);
MainContentTable.Rows.Add(tableRow);

In order for Unity to BuildUp the control it will have to know the actual type of control to build up which is why the UnderlyingSystemType is used as the Type argument in the BuildUp call. The alternative is to cast control to the appropriate type but that might not be known at compile time.

Sign up to request clarification or add additional context in comments.

1 Comment

I ended up inheriting all the controls from a generic class and moving the interfaces to that class. If someone needs my code, leave me a message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.