Note that there are some explanatory texts on larger screens.

plurals
  1. POUnity and ASP.NET WebForms - No parameterless constructor defined for this object
    primarykey
    data
    text
    <p>Does anyone have any good examples of how to make Unity 1.2 or 2.0 work with ASP.NET WebForms? </p> <p>I thought I had this figured out, but evidently I'm missing something. Now I'm getting the error; "No parameterless constructor defined for this object". I remember getting this error a couple years ago, I and just don't remember what I did.</p> <p>Obviously Unity isn't working as it should because somewhere along the way I've forgotten something. Any help would be appreciated.</p> <p>Here's some of my code:</p> <p>Global.asax</p> <pre> using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Security; using System.Web.SessionState; using Microsoft.Practices.Unity; using PIA35.Unity; namespace PIA35.Web { public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { IUnityContainer container = Application.GetContainer(); PIA35.Web.IoC.Bootstrapper.Configure(container); } } } </pre> <p>Here's my httpModules section of the web.config file:</p> <pre> &lt;httpModules&gt; &lt;add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/&gt; &lt;add name="UnityHttpModule" type="PIA35.Unity.UnityHttpModule, PIA35.Unity"/&gt; &lt;/httpModules&gt; </pre> <p>Here's the code for my IoC bootstrapper class.</p> <pre> using System; using System.Collections.Generic; using System.Linq; using System.Web; using Microsoft.Practices.Unity; using PIA35.Services.Interfaces; using PIA35.Services; using PIA35.DataObjects.Interfaces; using PIA35.DataObjects.SqlServer; namespace PIA35.Web.IoC { public static class Bootstrapper { public static void Configure(IUnityContainer container) { container .RegisterType&lt;ICategoryService, CategoryService&gt;() .RegisterType&lt;ICustomerService, CustomerService&gt;() .RegisterType&lt;IOrderService, OrderService&gt;() .RegisterType&lt;IOrderDetailService, OrderDetailService&gt;() .RegisterType&lt;IProductService, ProductService&gt;() .RegisterType&lt;ICategoryDao, SqlServerCategoryDao&gt;() .RegisterType&lt;ICustomerDao, SqlServerCustomerDao&gt;() .RegisterType&lt;IOrderDao, SqlServerOrderDao&gt;() .RegisterType&lt;IOrderDetailDao, SqlServerOrderDetailDao&gt;() .RegisterType&lt;IProductDao, SqlServerProductDao&gt;(); } } } </pre> <p>Here's the HttpApplicationStateExtensions.cs file.</p> <pre> using System.Web; using Microsoft.Practices.Unity; namespace PIA35.Unity { public static class HttpApplicationStateExtensions { private const string GlobalContainerKey = "GlobalUnityContainerKey"; public static IUnityContainer GetContainer(this HttpApplicationState application) { application.Lock(); try { IUnityContainer container = application[GlobalContainerKey] as IUnityContainer; if (container == null) { container = new UnityContainer(); application[GlobalContainerKey] = container; } return container; } finally { application.UnLock(); } } } } </pre> <p>Here's my UnityHttpModule.cs file.</p> <pre> using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using Microsoft.Practices.Unity; namespace PIA35.Unity { public class UnityHttpModule : IHttpModule { #region IHttpModule Members /// ///Initializes a module and prepares it to handle requests. /// /// ///An ///that provides access to the methods, properties, ///and events common to all application objects within an ASP.NET application public void Init(HttpApplication context) { context.PreRequestHandlerExecute += OnPreRequestHandlerExecute; } /// ///Disposes of the resources (other than memory) ///used by the module that implements . /// /// public void Dispose() { } #endregion private void OnPreRequestHandlerExecute(object sender, EventArgs e) { IHttpHandler handler = HttpContext.Current.Handler; HttpContext.Current.Application.GetContainer().BuildUp(handler.GetType(), handler); // User Controls are ready to be built up after the page initialization is complete Page page = HttpContext.Current.Handler as Page; if (page != null) { page.InitComplete += OnPageInitComplete; } } // Get the controls in the page's control tree excluding the page itself private IEnumerable GetControlTree(Control root) { foreach (Control child in root.Controls) { yield return child; foreach (Control c in GetControlTree(child)) { yield return c; } } } // Build up each control in the page's control tree private void OnPageInitComplete(object sender, EventArgs e) { Page page = (Page)sender; IUnityContainer container = HttpContext.Current.Application.GetContainer(); foreach (Control c in GetControlTree(page)) { container.BuildUp(c.GetType(), c); } } } } </pre> <p>Here's an example of one of my service classes.</p> <pre> namespace PIA35.Services { public class CategoryService : ICategoryService { #region Dependency Injection private ICategoryDao categoryDao; public CategoryService(ICategoryDao CategoryDao) { this.categoryDao = CategoryDao; } #endregion #region ICategoryService Members public List GetAll() { return categoryDao.GetAll().ToList(); } public Category GetById(int CategoryId) { return categoryDao.GetById(CategoryId); } public void Add(Category model) { categoryDao.Insert(model); } public void Update(Category model) { categoryDao.Update(model); } public void Delete(Category model) { categoryDao.Delete(model); } #endregion } } </pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload