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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:11:45+00:00 2026-05-23T06:11:45+00:00

Is it possible to easily configure autofac so it will only resolve using non-obsolete

  • 0

Is it possible to easily configure autofac so it will only resolve using non-obsolete constructors?

eg for a class with a helper constructor for non-DI code,

public class Example {

    public Example(MyService service) {
        // ...
    }

    [Obsolete]
    public Example() {
        service = GetFromServiceLocator<MyService>();
        // ...
    }
}

// ....

var builder = new ContainerBuilder();
builder.RegisterType<Example>();
// no MyService defined.
var container = builder.Build();

// this should throw an exception
var example = container.Resolve<Example>();

asking autofac to resolve Example if we haven’t registered MyService, should fail.

  • 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-23T06:11:46+00:00Added an answer on May 23, 2026 at 6:11 am

    I don’t believe there is an out of the box way to configure Autofac to ignore Obsolete constructors. However, Autofac is so good, there is always a way to get it done 🙂 Here are two options:

    Option 1. Tell Autofac which Constructor to use

    Do this using the UsingConstructor registration extension method.

    builder.RegisterType<Example>().UsingConstructor(typeof(MyService));
    

    Option 2. Provide a custom IConstructorFinder to FindConstructorsWith

    Autofac has a registration extension method called FindConstructorsWith. You can pass a custom IConstructorFinder to one of the two overloads. You could write a simple IConstructorFinder called NonObsoleteConstructorFinder that will only return constructors without the Obsolete attribute.

    I have written this class and added a working version of your sample. You can view the full code and use it as inspiration. IMO option this is the more elegant option. I have added it to my AutofacAnswers project on GitHub.

    Note: The other overload takes BindingFlags. I don’t think you can specify attribute requirements using BindingFlags. However, you may want to check that.

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

Sidebar

Related Questions

Is it possible to configure android.app.DatePickerDialog so that it is localized for the European
Is it possible to configure the Windows Workflow SqlWorkflowPersistanceService to use different SQL Server
I'm not sure if this is easily possible, but would be really handy if
I want to grep a word with line number. It's easily possible in the
I am working to configure Spring-Security with an existing application, for just basic security
When persisting domain objects using Grails/GORM I frequently find myself wondering why a save()
I was reading http://www.orchardproject.net/docs/Creating-1-n-and-n-n-relations.ashx and could not get the idea, if it is possible
I'd like to speed up junit tests and I think about using forkMode=once .
Possible Duplicate: Best way to parse command line arguments in C#? What's the best
Is it possible to make the fabfile stand-alone? I'm not very fond of running

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.