Resolve type in microsoft unity in a web application in global.asax

Oct 14, 2014 at 7:58 AM
Edited Oct 14, 2014 at 8:05 AM
i am trying to resolve some classes in my web application following this example (page42/43). But something seem does not work infact whne i try to call the method of my interface I obtain a nullreference exception.

Following my code... In Global.asax:
private IUnityContainer _container;

  protected void Application_Start(object sender, EventArgs e)
  {
        this._container = Application.GetContainer(); //GetContainer has been implemented by me, so it could be different from the example;

        this._container.LoadConfiguration(); //Here, it is totally different from the example.. I don't know how to implement ContainerBootstrapper.Configure(container);
  }

  protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
  {
        var handler =
            HttpContext.Current.Handler as System.Web.UI.Page;

        if (handler != null)
        {
            this._container = Application.GetContainer();

            if (this._container != null)
            {
                this._container.BuildUp(handler.GetType(), handler);
            }
        }
  }
Here how I implemented the gatContainer method:
public  static class bo {

    public static IUnityContainer GetContainer(this HttpApplicationState application)
    {
        IUnityContainer container = application["GlobalContainerKey"] as IUnityContainer;
        if (container == null)
        {
            application.Lock();
            try
            {
                container = application["GlobalContainerKey"] as IUnityContainer;
                if (container == null)
                {
                    container = new UnityContainer();
                    application["GlobalContainerKey"] = container;
                }
            }
            finally
            {
                application.UnLock();
            }
        }
        return container;
    }
}
Here my page:
[Dependency]
public IFacade.IPublicAreaFacade PublicFacade { get; set; }
And in a method in the same page:
//EasyPayResponse response = ((Global)this.Context.ApplicationInstance).PublicFacade.DoEasyPayPayment(request);


EasyPayResponse response = PublicFacade.DoEasyPayPayment(request); // <-- Here i have a nullreference excetion PublicFacade is null. Please note that the line before, that is commented, worked, but i don't like that approach. So i am trying do follow that one in the example, that surely is better. So I htink it is not a problem of configuration.
Can anyone help me please?

Thank you
Oct 14, 2014 at 8:39 AM
Based on what is posted, it should be working. LoadConfiguration is called so the container configuration is being loaded from XML. What does the configuration look like? Is the mapping between IFacade.IPublicAreaFacade and the implementation class defined?

Also, ensure that the Dependency attribute is using Microsoft.Practices.Unity.DependencyAttribute and not System.Runtime.CompilerServices.DependencyAttribute.

If you could post a short, complete example it would help pinpoint the problem quickly.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Oct 14, 2014 at 8:59 AM
Hi randy,
following my configuration:
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  </configSections>

  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <containers>
      <container>
        <register type="it.MC.IFacade.IPublicAreaFacade, IFacade" mapTo="it.MC.Facade.PublicAreaFacade, Facade">
          <!--<lifetime type="singleton" />-->
        </register>
      </container>
    </containers>
  </unity>
I think configuration is ok because when I did a resolve everything worked...

BEFORE I did in this way... In my global.asax:
//public IPublicAreaFacade PublicFacade
        //{
        //    get 
        //    { 
        //        return Application["_publicFacade"] as IPublicAreaFacade; 
        //    }
        //    private set 
        //    { 
        //        if(Application["_publicFacade"] == null)
        //            Application["_publicFacade"] = value; 
        //    }
        //}        

        protected void Application_Start(object sender, EventArgs e)
        {
            //this._container = ContainerFactory.GetNewContainer();

            //this.PublicFacade = this._container.Resolve<IPublicAreaFacade>();
        }
And in the page:
EasyPayResponse response = ((Global)this.Context.ApplicationInstance).PublicFacade.DoEasyPayPayment(request);
Everything worked but I really don't like this approach...

Yes, Dependency attribute is using Microsoft.Practices.Unity.DependencyAttribute...

I have some doubt on the GetContainer implementation and in ContainerBootstrapper.Configure(container). I have not implemented this second method...

PS.: What you mean with a complete example? Do you want i upload a little project?

Thank you
Oct 14, 2014 at 9:22 AM
Ah another doubt.... and perhaps the most important...

Here how should be the method buildup... but I have other overloads!!!

I am using this one:
buildup(Type t, params ResolverOverrides[] resolverOverrides)

... so, perhaps i am using an old version of unity???
Oct 14, 2014 at 10:06 AM
Uff. I am using runtime version v4 and Version 3.5... How can be possible I see different overloads?
Oct 14, 2014 at 3:41 PM
The issue is that the PublicFacade is defined on the HttpApplication level but you are "building up" the Page. Usually the dependency would be defined on a Page level and the property would be injected. I would move the PublicFacade dependency into the Page(s) that depend on it. Then when BuildUp is called on the Page the PublicFacade singleton would be injected into the page.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Oct 14, 2014 at 4:05 PM
Ohhhh you are great... The problem was not exactly that.. perhaps it was not clear in the code I posted.
I had defined the PublicFacade inside a user control!!! Not inside the page...

I moved all the code in the page and now everything work correctly... But how could I do if i want to define it inside my user controls???
In this way it would mean I should replicate code in different pages :(

Any suggestion?

Thanx
Oct 14, 2014 at 4:20 PM
For more complicated scenarios follow the approach using the HttpModule in the article ASP.NET Web Forms Applications.

Basically, the approach is to find all controls on the page and perform a BuildUp on all the controls.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Oct 15, 2014 at 7:53 AM
Great thank you, even if I am a little bewildered about performance... A cycle on each control of the page....