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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:37:17+00:00 2026-06-07T18:37:17+00:00

I seem to be doing this a fair bit in my code: public class

  • 0

I seem to be doing this a fair bit in my code:

public class ActionsModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            base.Load(builder);
            builder.Register(c => LogManager.GetCurrentClassLogger()).As<ILog>().InstancePerDependency();

            // Autofac doesn't seem to be able to inject things without explicit binding 
            builder.RegisterType<ComboActions>().As<ComboActions>().InstancePerHttpRequest();
            builder.RegisterType<AppActions>().As<AppActions>().InstancePerHttpRequest();
        }
    }
}

Where the ‘actions’ class is a class I require to be injected into my controller, and has various other sub-dependencies.

Seems a bit rubbish. Why can’t autofac resolve that the class has a constructor with dependencies that are already satisfied and manufacture an instance automatically?

I mean, if class A requires class B to be injected and class B requires C, D, E, etc. fair enough, I guess you dont want to walk the entire dependency chain to see if you can make a class at run time. …but if class A directly depends on C and D which are explicitly bound, surely that’s a trivial case?

Am I missing something? Can’t seem to see any documentation for 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-06-07T18:37:18+00:00Added an answer on June 7, 2026 at 6:37 pm

    AFAIK, Autofac requires every needed type to be registered in the container, but this does not mean you have to do each one individually. Almost 99% of my registrations are handled by adding this attribute to the type:

    [AttributeUsage(AttributeTargets.Class)]
    public class AutoRegisterAttribute : Attribute { }
    

    So for example, you’d have

    [AutoRegister]
    class ComboActions
    {
    

    And then I register them with this:

    public class AutoScanModule : Module
    {
        private readonly Assembly[] _assembliesToScan;
    
        public AutoScanModule(params Assembly[] assembliesToScan)
        {
            _assembliesToScan = assembliesToScan;
        }
    
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterAssemblyTypes(_assembliesToScan)
                .Where(t => t.GetCustomAttributes(typeof (AutoRegisterAttribute), false).Any())
                .AsSelf()
                .AsImplementedInterfaces()
                .InstancePerLifetimeScope();
        }
    }
    

    Like I said, this covers most of my registrations and then I usually only have to worry about things like 3rd party types, open generics and decorators.

    EDIT: Check out the reply from Kaleb that proves me wrong. Cool feature I never knew about!

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

Sidebar

Related Questions

I can't seem to find anywhere that talks about doing this. Say I have
I can't seem to find an easy way of doing this. We have a
I can't seem to find a way to fix this problem. All I'm doing
I am doing this in my view : <div class=eight columns> <div class=content> <%=
This might seem a bit awkward but I want to start a console program
I thought I was doing this right but it doesn't seem to be working.
I can’t seem to find a way of doing this, but it’s such a
Could someone explain why is my strcat doing this? I can't seem to find
I've recently seen several people doing things like this here on Stackoverflow: class A:
I've recently seen several people doing things like this here on Stackoverflow: class A:

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.