Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with Unity BuildUp Method

I'm using Unity App Block for my project (version 1.2.0.0). I have a problem with Unity Container BuildUp method which I'm using for my ascx controls. Here is some code (that's pretty simple)

public class BaseUserControl<T>:UserControl where T:class
    {
 protected override void OnInit(EventArgs e)
        {
            InjectDependencies();
            base.OnInit(e);
        }

 protected virtual void InjectDependencies()
            {
         var context = HttpContext.Current;
                if (context == null)
                {
                    return;
                }
                var accessor = context.ApplicationInstance as IContainerAccessor;
                if (accessor == null)
                {
                    return;
                }
                var container = accessor.Container;
                if (container == null)
                {
                    throw new InvalidOperationException("No Unity container found");
                }
                container.BuildUp<T>(this as T);

      }
}

This method is called in base control for ascx controls in my solution. And here the property that should be injected in child control:

 [Dependency]
  private IStock Stock { get; set; }

So after buildup Stock property is still empty. Resolve method works fine for IStock with the same container and configuration. I've tried buildup with simple test class with only one property IStock and got the same result. So what can be wrong with buildup?

like image 258
Voice Avatar asked Feb 10 '26 17:02

Voice


1 Answers

Found what was wrong with my code. The problem was pretty simple: property IStock should be public or in other case buildup will not populate it with registered value. Hope it'll help somebody in the future.

like image 121
Voice Avatar answered Feb 13 '26 22:02

Voice