Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3287236
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:28:13+00:00 2026-05-17T20:28:13+00:00

Does anyone have any good examples of how to make Unity 1.2 or 2.0

  • 0

Does anyone have any good examples of how to make Unity 1.2 or 2.0 work with ASP.NET WebForms?

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.

Obviously Unity isn’t working as it should because somewhere along the way I’ve forgotten something. Any help would be appreciated.

Here’s some of my code:

Global.asax

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);
        }
    }
}

Here’s my httpModules section of the web.config file:

<httpModules>
    <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add name="UnityHttpModule" type="PIA35.Unity.UnityHttpModule, PIA35.Unity"/>
</httpModules>

Here’s the code for my IoC bootstrapper class.

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<ICategoryService, CategoryService>()
                .RegisterType<ICustomerService, CustomerService>()
                .RegisterType<IOrderService, OrderService>()
                .RegisterType<IOrderDetailService, OrderDetailService>()
                .RegisterType<IProductService, ProductService>()
                .RegisterType<ICategoryDao, SqlServerCategoryDao>()
                .RegisterType<ICustomerDao, SqlServerCustomerDao>()
                .RegisterType<IOrderDao, SqlServerOrderDao>()
                .RegisterType<IOrderDetailDao, SqlServerOrderDetailDao>()
                .RegisterType<IProductDao, SqlServerProductDao>();
        }
    }
}

Here’s the HttpApplicationStateExtensions.cs file.

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();
            }
        }
    }
}

Here’s my UnityHttpModule.cs file.

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);
            }
        }
    }
}

Here’s an example of one of my service classes.

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
    }
}
  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:28:13+00:00Added an answer on May 17, 2026 at 8:28 pm

    I see it has already been answered but just thought I would point out that you are synchronising all the calls to GetContainer with your locking pattern. A call to Application.Lock() actually takes out a write lock on the applicationState which is a singleton object in your web application and you will see issues if you want to scale this.

    To tidy this up you could do a double checked lock. like this:

        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;
        }
    

    I would also like to point out a neat pattern that we have used to ensure Controls and Pages have their Dependencies built up. We basically have a Generic PageBase and Generic ControlBase that all our pages and controls inherit from. I will just go into the pagebase as an example:

    public abstract class SitePageBase<T> : SitePageBase where T : SitePageBase<T>
    {
        protected override void OnInit( EventArgs e )
        {
            BuildUpDerived();
            base.OnInit( e );
        }
    
        protected void BuildUpDerived()
        {
            ContainerProvider.Container.BuildUp( this as T );
        }
    }
    

    Then in our Pages we can simply derive from Generic base and it will look after the build up.

    public partial class Default : SitePageBase<Default>
    {
            [Dependency]
            public IContentService ContentService { get; set; }
    
            protected override void OnPreRender( EventArgs e )
            {
                this.label.Text = ContentService.GetContent("labelText");
            }
         }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

IS this straightforward? Does any one have any good examples? All my google searches
Scenario Does anyone have any good examples of peer-to-peer (p2p) networking in C++ using
Does anyone have any good examples of calling a WCF service from a classic
Does anyone have any good examples of building a tree data structure both iteratively
Does anyone have any good articles or explanations (blogs, examples) for pointer arithmetic? Figure
DOes anyone have any good information with regards to when to log, I was
Does anyone have any good advice or experience on how to create an engine
Does anyone have any good samples, resources, pointers etc. for C# Silverlight with RIA
Does anyone have any good sources of information of using NHibernate with Sql Azure
Does anyone have any pointers to good resources concerning mesh networks? Maybe I'm not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.