add tag
kushalgmx
I have a sample repository on github, specifically this commit https://github.com/kushalgmx/FileTransfer/commit/fb469e7fbabe05721193caa0b19bf1a01f99cd6a 

I started this project thinking I will test drive Visual Studio 2022. In any case, I figured I'd ask this question:

How do I pass connection string from console project to class library project? Or maybe that's not even the right question. Perhaps a better question is who does the app config belong to? 

My initial thought is the application configuration belongs to the application. It is right in the name, right? I am thinking maybe I could have two console application projects. Both of them should be able to run at the same time and pass their own connection string and both should just work with each having no idea there are other console application projects? 

For the sake of completeness, here is the code 

Console application, C# 

```csharp
using FileTransfer.Persistence;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;

namespace FileTransfer.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            var host = AppStartup();

            var dataService = ActivatorUtilities.CreateInstance<DataService>(host.Services);

            dataService.Connect();
        }

        static void ConfigSetup(IConfigurationBuilder builder)
        {
            builder.SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                    .AddEnvironmentVariables();
        }

        static IHost AppStartup()
        {
            var builder = new ConfigurationBuilder();
            ConfigSetup(builder);

            // defining Serilog configs
            Log.Logger = new LoggerConfiguration()
                .ReadFrom.Configuration(builder.Build())
                .Enrich.FromLogContext()
                .WriteTo.Console()
                .WriteTo.File(@"C:\Users\khada\source\personal\FileTransfer\FileTransfer.Console\log.txt", rollingInterval: RollingInterval.Day)
                .CreateLogger();

            // Initiated the dependency injection container 
            var host = Host.CreateDefaultBuilder()
                        .ConfigureServices((context, services) =>
                        {
                            services.AddTransient<IDataService, DataService>();
                        })
                        .UseSerilog()
                        .Build();

            return host;
        }
    }
}
```

Interface, library

```
namespace FileTransfer.Persistence
{
    public interface IDataService
    {
        void Connect();
    }
}
```

Class, library

```
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace FileTransfer.Persistence
{
    public class DataService: IDataService, IDisposable
    {
        private readonly ILogger<DataService> _log;
        private readonly string _connectionString;

        public DataService(
            ILogger<DataService> log,
            string connectionString)
        {
            _log = log;
            _connectionString = connectionString;
        }

        public void Connect()
        {
            _log.LogInformation($"TODO: Write code to connect to Connection String {_connectionString}");
        }

        public void Disconnect()
        {
            _log.LogDebug($"TODO: Write code to disconnect from Connection String {_connectionString} here.");
        }

        public void Dispose()
        {
            Disconnect();
        }
    }
}
```
Top Answer
Josh Darnell
> Perhaps a better question is who does the app config belong to?
> 
> My initial thought is the application configuration belongs to the application.

That's right.  You have the connection string defined in the right place (appSettings.json of the Console application).

As far as how to pass it in, you have defined a required "connectionString" parameter on the `DataService` class, which is also good.  You just need to configure the dependency injection to pass it in.

That happens here:

```
// Initiated the dependency injection container 
var host = Host.CreateDefaultBuilder()
            .ConfigureServices((context, services) =>
            {
                services.AddTransient<IDataService, DataService>((services) =>
                {
                    return new DataService(
                        services.GetRequiredService<ILogger<DataService>>(), 
                        services
                            .GetRequiredService<IConfiguration>()
                            .GetConnectionString("DefaultConnection"));
                });
            })
            .UseSerilog()
            .Build();
```

In this case, I've chosen to define exactly how the DI container will get the connection string: by resolving the `IConfiguration` service, and then getting it from there (that is, from your appSettings.json).

Then your `Main` method needs to be updated to do this:

```
static void Main(string[] args)
{
    var host = AppStartup();

    var dataService = host.Services.GetRequiredService<IDataService>();

    dataService.Connect();
}
```

Notice that I'm just resolving the `IDataService` dependency we already configured, rather than using the Activator to try to instantiate it with reflection.

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.