user2503078
user2503078

Reputation: 807

Pass multiple connection strings through IServicesCollection.AddScoped<T>()

.net core webapi application with entityframeworkcore. Trying to figure out how to pass an additional connection string into the data access library class in addition to the dbcontext. See ** dbcontext ** below is what I want to do.

startup.cs

       var SqlConnection = Configuration.GetConnectionString("SQLConnection");  
       var BlobConnection = Configuration.GetConnectionString("BlobConnection");

        services.AddEntityFramework()
                .AddEntityFrameworkSqlServer()
                .AddDbContext<VISTrackingContext>(options => options.UseSqlServer(SqlConnection, 
                sqlServerOptionsAction: sqlOptions =>
                {
                    sqlOptions.EnableRetryOnFailure(maxRetryCount: 5,
                    maxRetryDelay: TimeSpan.FromSeconds(5),
                    errorNumbersToAdd: null);
                }));

        services.AddScoped<IDataManager>(c => new DataManager(**_dbcontext**, BlobConnection));

        public FileController(IDataManager manager)
        {
            _datamanager = manager;
        }

        public DataManager(VISTrackingContext dbcontext, string blobconnection)
        {
            _dbcontext = dbcontext;
            _blobconnection = blobconnectionstring;
        }

Can this be done? Or is there another way to inject an additional connection string through the context object itself? I see lots of comments and options on doing this but none of the approaches deal with both the context and the connection string being passed to the repository object.

Upvotes: 0

Views: 1210

Answers (2)

Tseng
Tseng

Reputation: 64239

Alternatively to @Win's answer, a more generic approach:

public class ConnectionStrings : Dictionary<string,string> { }

Startup.cs

services.Configure<ConnectionStrings>(Configuration.GetSection("ConnectionStrings"));
service.AddScoped<IDataManager,DataManager>();

// or
services.AddScoped<IDataManager>(c => new DataManager(
    c.GetRequiredService<YourDbContext>(),
    c.GetRequiredService<ConnectionStrings>()["BlobConnection"])
);

Service

public DataManager(VISTrackingContext dbcontext, ConnectionStrings connectionStrings)

Upvotes: 0

Win
Win

Reputation: 62290

You could store those connection strings in an object, and inject it to DataManager.

For example,

public class Startup
{
   public void ConfigureServices(IServiceCollection services)
   {
      ...
      services.AddSingleton(provider => new DataSettings(
         Configuration.GetConnectionString("SQLConnection"), 
         Configuration.GetConnectionString("BlobConnection")));

      services.AddScoped<IDataManager, DataManager>();
      ...
   }
}

public class DataSettings
{
    public DataSettings(string sqlConnection, string blobConnection)
    {
        SQLConnection = sqlConnection;
        BlobConnection = blobConnection;
    }

    public string SQLConnection { get; }

    public string BlobConnection { get; }
}

public DataManager(VISTrackingContext dbcontext, DataSettings dataSettings)
{
   _dbcontext = dbcontext;
   _blobconnection = dataSettings.BlobConnection;
}

Upvotes: 1

Related Questions