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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:44:01+00:00 2026-06-12T14:44:01+00:00

I have a problem when trying to inject one dependency into the one I’m

  • 0

I have a problem when trying to inject one dependency into the one I’m currently registering. I have an application that upon starting inits a bootstrap loader to read from a settings file, and then creates an object using reflection from a particular value in this settings file. I’ll try and let the code do the talking here.

Settings file

The settings file is a plain old XML file, that serializes / deserializes to an object called BootstrapSettings which adheres to the following contract:

public interface IBootstrapSettings
{
    string Get(string key);
    void Set(string key, string value);
}

Within this settings file, I have a particular key that identifies what IEncryptor I intend to use for this application, at the moment I have a choice of PlainTextEncryptor or TDESEncryptor, the latter reads a passphrase from the settings file and so in its constructor I am passing an instance of IBootstrapSettings to read this passphrase from the settings file. At the moment I am injecting an instance of IBootstrapSettings into each encryptor – even though PlainTextEncryptor doesn’t use it. I’m doing this because I’m having to create this instance using reflection and don’t know how I can differentiate between a constructor that doesn’t take a parameter and a constructor that does when creating an IEncryptor type (but this is another question I suppose).

IEncryptor constructors

The following are the constructors for each IEncryptor I currently have.

public sealed class PlainTextEncryptor : IEncryptor
{
    private readonly IBootstrapSettings _bootstrapSettings;

    public PlainTextEncryptor(IBootstrapSettings bootstrapSettings)
    {
        _bootstrapSettings = bootstrapSettings;
    }
}

public sealed class TDESEncryptor : IEncryptor
{
    private readonly IBootstrapSettings _bootstrapSettings;

    public TDESEncryptor(IBootstrapSettings bootstrapSettings)
    {
        _bootstrapSettings = bootstrapSettings;
    }
}

Injection

Onto how I’m setting up my dependencies. I have an Autofac module that creates the bootstrap dependencies before anything else is created – it looks like this:

public sealed class BootstrapSettingsModule : Autofac.Module
{
    private readonly string _filePath;

    public BootstrapSettingsModule(string filePath)
    {
        _filePath = filePath;
    }

    protected override void Load(ContainerBuilder builder)
    {
        base.Load(builder);

        builder.Register(c => new BootstrapSettings(_bootstrapSettingsPath))
            .As<IBootstrapSettings>()
            .SingleInstance();

        builder.Register(c => (IEncryptor)Activator.CreateInstance(Type.GetType(c.Resolve<IBootstrapSettings>().Get("encryptionprovider")), args: new { bootstrapSettings = c.Resolve<IBootstrapSettings>() }))
            .As<IEncryptor>()
            .SingleInstance();
    }
}

Now this executes fine, the problem occurs when I try to .Resolve an instance of IEncryptor I am getting the error Constructor on type ‘whatever encryptor type’ not found. I assume this is because Autofac cannot resolve my IBootstrapSettings and because I have no default parameterless constructor on my IEncryptor it’s throwing a fit.

I’m at a bit of a loss as to what to do here, am I doing something wrong by specifying the IBootstrapSettings as a single instance… or do I just not understand Autofac’s binding correctly? Appreciate any help as always!

  • 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-12T14:44:02+00:00Added an answer on June 12, 2026 at 2:44 pm

    I don’t understand why you are not just using the facilities of Autofac.

    protected override void Load(ContainerBuilder builder)
    {
        base.Load(builder);
    
        var settings = new BootstrapSettings(_bootstrapSettingsPath);
        builder.RegisterInstance(settings)
            .As<IBootstrapSettings>()
            .SingleInstance();
    
        var encryptorType = Type.GetType(settings.Get("encryptionprovider"));
        builder.RegisterType(encryptorType)
            .As<IEncryptor>()
            .SingleInstance();
    }
    

    This would give you the additional advantage that you wouldn’t need to have a constructor with an IBootstrapSettings parameter in every encryptor. You would need it just in those that really make use of that parameter.

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

Sidebar

Related Questions

I have a problem trying to turn my python code into an executable using
I'm trying to inject some CSS that accompanies some other HTML into a C#
I have a problem with castle core, i'm trying to inject two different database
I have a problem trying to register my own Event/Listener to the event dispatcher.
I have a problem trying to design some generic storage.. Basically I have the
I have a problem trying to edit. I work with Areas for better management
I have a problem trying to hide .php extension from the url I have
I implemented the table sorter yesterday and i have a problem trying to change
I have a problem when trying to count in PHP using a button and
I have a problem when trying to execute this update statement (below) using C#

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.