Getting null reference error while implementing Dependency Injection in ASP.NET WebForms using Unity. RRS feed

  • Question

  • User906238278 posted

    Hi Team,

    Getting null reference error while implementing Dependency Injection in ASP.NET WebForms using Unity.

     In my Asp.net Web Application I am using generic repository pattern and unit of work with dependency injection  ,

    To achieve dependency injection in asp.net web forms using Unity. I run the following command in Package Manager Console


    Install-Package Unity.WebForms results the required dll's are referred to my project(Unity.WebForms ) & UnityWebFormsStart.cs is add to my project with App_Start Folder.


    I Register the following object in RegisterDependencies Method that present in UnityWebFormsStart.cs

                                                     internal static void PostStart()


                                                                            IUnityContainer container = new UnityContainer();

                                                                            HttpContext.Current.Application.SetContainer( container );

                                                                             RegisterDependencies( container );


                           private static void RegisterDependencies( IUnityContainer container )


      // TODO: Add any dependencies needed here

                container.RegisterType<IDeptPresenter, DeptPresenter>();

                container.RegisterType<IServiceDepts, ServiceDepts>();

                container.RegisterType<IRepDepts, RepDepts>();

                container.RegisterType<IUnitOfWork, UnitOfWork>();

                container.RegisterType<IDeptView, DeptView>();



    And I call UnityWebFormsStart in Application_Start Global.asax.cs

     protected void Application_Start(object sender, EventArgs e)





    In UI I implement DI through Constructor

    public partial class DeptList : System.Web.UI.Page, IDeptView


    private readonly IDeptPresenter ObjDeptPresenter;     

            public DeptList()

            {             }

            public DeptList(IDeptPresenter _ObjDeptPresenter)


                this.ObjDeptPresenter = _ObjDeptPresenter;


            protected void Page_Load(object sender, EventArgs e)





    But at page_Load at ObjDeptPresenter getting null reference error

    “Object reference not set to an instance of an object.


    Could You Please kindly help me where i made the mistake & help me out to resolve the issue.

    Your support/suggestions are highly appreciated and welcome.
    Pradan Prasant Kumar .

    Wednesday, August 13, 2014 2:40 AM

All replies

  • User108214351 posted

    Dependency Injection in ASP.NET doesn't work with constructors because the ASP.NET pipeline doesn't allow you to intercept object (page) creation. You will need to have a public property for the type you are trying to inject and then add the "[Dependency]" attribute to that property. More info can be found here (the project source and information for using Unity.WebForms:  https://bitbucket.org/KyleK/unity.webforms/wiki/Home

    - Kyle K

    Thursday, August 21, 2014 7:58 PM