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

  • Home
  • SEARCH
  • 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 6052891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:58:44+00:00 2026-05-23T07:58:44+00:00

I use the RegisterInitializer method to inject properties in base types as follows: container.RegisterInitializer<BaseHandler>(handler

  • 0

I use the RegisterInitializer method to inject properties in base types as follows:

container.RegisterInitializer<BaseHandler>(handler =>
{
    handler.Context = container.GetInstance<HandlerContext>();
});

This works great, but the RegisterInitializer doesn’t get fired on all registered types that inherit from BaseHandler. It doesn’t seem to run when I call new myself:

var url = ConfigurationManager.AppSettings["NotificationServiceUrl"];
container.Register<Handler<NotifyCustomerMessage>>(() => 
    new NotifyCustomerHandler(url));

// many other Handler<T> lines here

Why is that, and how can I solve this?

  • 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-23T07:58:44+00:00Added an answer on May 23, 2026 at 7:58 am

    UPDATE

    This behavior has changed in Simple Injector 2: Initializers will now also fire on Func<T> registrations. The reason is that Simple Injector now has explicit lifestyle support and now in fact behaves like StructureMap (as described below).


    Your observation is correct. The Simple Injector documentation describes it like this:

    Note: The container will not be able
    to call an initializer delegate on a
    type that is manually constructed
    using the new operator. Use automatic
    constructor injection whenever
    possible.

    The best way to overcome this problem is allowing Simple Injector to use automatic constructor injection.

    Your NotifyCustomerHandler takes a string constructor argument, which makes it impossible to do automatic constructor injection. Your NotifyCustomerHandler seems to have multiple responsibilities. Abstract the notification service from the handler functionality, by hiding this service behind an INotificationService interface and letting the handler depend on that interface. You can than inject that configuration value into the notification service.

    Some background information on why Simple Injector behaves this way

    Although other DI frameworks (such as StructureMap with its OnCreationForAll method) will invoke the delegate even when you new up a type, Simple Injector does not. It is caused by the difference in which the frameworks expect users to register lifestyle.

    With StructureMap, lifestyles are configured by explicitly calling the LifecycleIs method. With Simple Injector users are expected to configure lifestyles by registering delegates that implement lifestyle themselves. Look for instance at the Per Thread lifestyle example in the documentation. With StructureMap it is the framework that controls the lifetime for you, while with Simple Injector it is often up to the user.

    In other words, StructureMap expects a registered delegate to always create a new instance, while Simple Injector does not have this expectation. Because StructureMap expects a new instance is returned, it can safely initialize that object after calling the delegate. Caching of instances is done elsewhere. Simple Injector however, will not call an initializer on objects returned from such delegate, simply because that would possibly reinitialize the same object over and over again, which could cause unexpected application behavior and possible performance problems.

    I hope this helps.

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

Sidebar

Related Questions

use javascript or other method ? have any recommendation?
I use this LINQ query in an app I am writing: internal int GetNoteCount(DateTime
I want to use a temp directory that will be unique to this build.
Use trap to capture signals like this: i=-1;while((++i<33)); do trap echo $i >> log.txt
I use the JAXBContext.newInstance operation in my JBoss based web application. This operation, as
'''use Jython''' import shutil print dir(shutil) There is no, shutil.move, how does one move
Use case: A does something on his box and gots stuck. He asks B
Use case: 3rd party application wants to programatically monitor a text file being generated
We use a data acquisition card to take readings from a device that increases
I use Firebug and the Mozilla JS console heavily, but every now and then

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.