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 3214182
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:01:49+00:00 2026-05-17T15:01:49+00:00

I am trying to work out a way to use dependency injection with ASP.NET

  • 0

I am trying to work out a way to use dependency injection with ASP.NET Web Forms controls.

I have got lots of controls that create repositories directly, and use those to access and bind to data etc.

I am looking for a pattern where I can pass repositories to the controls externally (IoC), so my controls remain unaware of how repositories are constructed and where they come from etc.

I would prefer not to have a dependency on the IoC container from my controls, therefore I just want to be able to construct the controls with constructor or property injection.

(And just to complicate things, these controls are being constructed and placed on the page by a CMS at runtime!)

Any thoughts?

  • 1 1 Answer
  • 0 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-17T15:01:50+00:00Added an answer on May 17, 2026 at 3:01 pm

    UPDATE 2019:
    With the introduction of Web Forms 4.7.2, there is now better support for DI. This invalidates the below. See: Wiring up Simple Injector in WebForms in .NET 4.7.2

    You can use automatic constructor injection by replacing the default PageHandlerFactory with a custom one. This way you can use an overloaded constructor to load the dependencies. Your page might look like this:

    public partial class HomePage : System.Web.UI.Page
    {
        private readonly IDependency dependency;
    
        public HomePage(IDependency dependency)
        {
            this.dependency = dependency;
        }
    
        // Do note this protected ctor. You need it for this to work.
        protected HomePage () { }
    }
    

    Configuring that custom PageHandlerFactory can be done in the web.config as follows:

    <?xml version="1.0"?>
    <configuration>
      <system.web>
        <httpHandlers>
          <add verb="*" path="*.aspx"
            type="YourApp.CustomPageHandlerFactory, YourApp"/>
        </httpHandlers>
      </system.web>
    </configuration>
    

    Your CustomPageHandlerFactory can look like this:

    public class CustomPageHandlerFactory : PageHandlerFactory
    {
        private static object GetInstance(Type type)
        {
            // TODO: Get instance using your favorite DI library.
            // for instance using the Common Service Locator:
            return Microsoft.Practices.ServiceLocation
                .ServiceLocator.Current.GetInstance(type);
        }
    
        public override IHttpHandler GetHandler(HttpContext cxt, 
            string type, string vPath, string path)
        {
            var page = base.GetHandler(cxt, type, vPath, path);
    
            if (page != null)
            {
                // Magic happens here ;-)
                InjectDependencies(page);
            }
    
            return page;
        }
    
        private static void InjectDependencies(object page)
        {
            Type pageType = page.GetType().BaseType;
    
            var ctor = GetInjectableCtor(pageType);
    
            if (ctor != null)
            {
                object[] arguments = (
                    from parameter in ctor.GetParameters()
                    select GetInstance(parameter.ParameterType)
                    .ToArray();
    
                ctor.Invoke(page, arguments);
            }
        }
    
        private static ConstructorInfo GetInjectableCtor(
            Type type)
        {
            var overloadedPublicConstructors = (
                from constructor in type.GetConstructors()
                where constructor.GetParameters().Length > 0
                select constructor).ToArray();
    
            if (overloadedPublicConstructors.Length == 0)
            {
                return null;
            }
    
            if (overloadedPublicConstructors.Length == 1)
            {
                return overloadedPublicConstructors[0];
            }
    
            throw new Exception(string.Format(
                "The type {0} has multiple public " +
                "ctors and can't be initialized.", type));
        }
    }
    

    Downside is that this only works when running your side in Full Trust. You can read more about it here. But do note that developing ASP.NET applications in partial trust seems a lost cause.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to work out a way of passing the web current http context
I'm trying to work out a graceful way of solving this problem. I have
Im trying to work out the best way scale my site, and i have
Hi I have been trying to work out the best way to do this
I am trying to work out the best way of doing this but not
Ok, so im trying to work out the fastest way of storing data on
I've been trying to work out the 'best practices' way to manage file uploads
i am trying to work this out is the simplest way, i am a
I've been trying to figure out an efficient way to work without reading twice
I'm trying to work out what a BizTalk call to a web service is

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.