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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:16:01+00:00 2026-06-02T17:16:01+00:00

I am using Autofac and I have several classes which ask for parameter of

  • 0

I am using Autofac and I have several classes which ask for parameter of type string and name lang. Is there a way to register a string value to all parameters named “lang”, so it resolves automatically? I do not want to edit any of the constructors, since it is not my code (I know accepting e.g. CultureInfo would make registration easy..)

Something resulting in short syntax like
builder.Register(lang => “en-US”).As().Named(“lang”)

would be ideal.

Thank you.

  • 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-02T17:16:04+00:00Added an answer on June 2, 2026 at 5:16 pm

    A fairly simple way to solve this is with a custom Autofac module.

    First, implement the module and handle the IComponentRegistration.Preparing event. This is also where you’ll store the value for your parameter:

    using System;
    using Autofac;
    using Autofac.Core;
    
    public class LangModule : Module:
    {
      private string _lang;
      public LangModule(string lang)
      {
        this._lang = lang;
      }
    
      protected override void AttachToComponentRegistration(
        IComponentRegistry componentRegistry,
        IComponentRegistration registration)
      {
        // Any time a component is resolved, it goes through Preparing
        registration.Preparing += InjectLangParameter;
      }
    
      protected void InjectLangParameter(object sender, PreparingEventArgs e)
      {
        // Add your named parameter to the list of available parameters.
        e.Parameters = e.Parameters.Union(
          new[] { new NamedParameter("lang", this._lang) });
      }
    }
    

    Now that you have your custom module, you can register it along with your other dependencies and provide the value you want injected.

    var builder = new ContainerBuilder();
    builder.RegisterModule(new LangModule("my-language"));
    builder.RegisterType<LangConsumer>();
    ...
    var container = builder.Build();
    

    Now when you resolve any type that has a string parameter “lang” your module will insert the value you provided.

    If you need to be more specific, you can use the PreparingEventArgs in the event handler to determine things like which type is being resolved (e.Component.Activator.LimitType), etc. Then you can decide on the fly whether to include your parameter.

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

Sidebar

Related Questions

I am using Autofac 2.4.5.724 under vb.net 2010. I have several classes without default
I have been looking for a way to register a factory class in autofac
I have run into an issue while creating a data service and using Autofac
I have an MVC3 app using Autofac and a custom membership provider. If I
I have been using autofac with MVC 3 for a while and love it.
I can see how I can instantiate objects of classes using autofac, and then
I'm using Autofac with ASP.NET MVC so all my controllers have their dependencies resolved
I'm using autofac framework with xml configuration. I have a question, here is the
I'm using Autofac 2.4.4.705. The output of the following code is: 1 (which means
I have the following registration builder.Register<Func<Type, IRequestHandler>>( c => request => (IRequestHandler)c.Resolve(request)); Basically I

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.