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
Step:-1
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.
Step:-2
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>();
}
Step:-3
And I call UnityWebFormsStart in Application_Start Global.asax.cs
protected void Application_Start(object sender, EventArgs e)
{
UnityWebFormsStart.PostStart();
}
Step:-4
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)
{
ObjDeptPresenter.GetAllDepts().Count();
}
}
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.
Thank's
Pradan Prasant Kumar .