1

I have a console app A and a web app B where A needs to use a service class in B. To deal with dependencies I tried to make an autofac module in B that is registered in A's Main method:

public class ImageServiceModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterType<ImageService>().As<IImageService>();
        ...

        var configurationBuilder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json");
        var configuration = configurationBuilder.Build();

        var services = new ServiceCollection();
        services.Configure<StorageSettings>(configuration.GetSection("StorageSettings"));

        builder.Populate(services);
    }
}

Now, the problem is that since the program is run in the context of A, B reads appSettings from A...

Is there a way to make the services registered in the module read from B's appsettings instead? Or is autofac modules a wrong approach to use here in the first place?

Agnete
  • 53
  • 1
  • 8

2 Answers2

0

Keep in mind that is very common to use .config file transformations in deploy pipelines, so, do you really want to deploy two different apps with different configurations? If not, putting the confing in your running application should make sense.

Let me know what you think! Rgrds!

  • The web app's appsettings contains database credentials and other very database specific settings that only are used in the repository called from B's service. Isn't it a bit weird to add these settings to A (that never uses any of these settings directly)? – Agnete Jan 22 '20 at 13:43
0

Config file is designed to be per application. That means you will have a single configuration file for each application. In your case you will have a configuration file for your console app and another one for your web app.

If some settings applied for both app it is common to duplicate these settings in each configuration file.

IF you want to have a single configuration file for part of your application you can create a library project with a custom settings file.

Let's say you have A and B, you can create a new library project named C which will be referenced by both A and C. Then in C you can create a c.settings.json file and in your configuration file and use it. In order to let the build process copy the c.settings.json file automatically to each project you can set the copy to Output Directory property in Visual Studio to Copy always

Your solution should look something like this :

enter image description here

enter image description here

Cyril Durand
  • 15,834
  • 5
  • 54
  • 62