C
C#16mo ago
Nickolaki

❔ ConfigureTestServices not overriding connection string value :(

Hi! I'm so lost as to why this isn't working. Currently the RemoveDbContext removes the service as expected. And I get no exceptions from re-adding with the new config. I have debugged that if I change my development appsettings to the hardcoded connection string from the created container it works fine. But as soon as I change dev connection string back to what it is I get db login issues/couldn't establish connection issues. So it must not be overriding the config. Any ideas on how I can fix this?
public class JobbyHttpApiFactory : WebApplicationFactory<Program>, IAsyncLifetime
{
public string JobbyDbConnectionString { get; set; } = string.Empty;
private readonly MsSqlContainer _mssqlContainer;
private readonly IConfiguration _configuration;
public HttpClient HttpClient { get; private set; } = null!;

public async Task InitializeAsync()
{
await _mssqlContainer.StartAsync();

JobbyDbConnectionString = _mssqlContainer.GetConnectionString();
}

public new async Task DisposeAsync()
{
...
}

protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureTestServices(services =>
{
services.RemoveDbContext<JobbyDbContext>();

services.AddDbContext<JobbyDbContext>(options =>
{
options.UseSqlServer(_mssqlContainer.GetConnectionString());
});

services.EnsureDbCreatedAsync<JobbyDbContext>().ConfigureAwait(true);
});
}
}
public class JobbyHttpApiFactory : WebApplicationFactory<Program>, IAsyncLifetime
{
public string JobbyDbConnectionString { get; set; } = string.Empty;
private readonly MsSqlContainer _mssqlContainer;
private readonly IConfiguration _configuration;
public HttpClient HttpClient { get; private set; } = null!;

public async Task InitializeAsync()
{
await _mssqlContainer.StartAsync();

JobbyDbConnectionString = _mssqlContainer.GetConnectionString();
}

public new async Task DisposeAsync()
{
...
}

protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureTestServices(services =>
{
services.RemoveDbContext<JobbyDbContext>();

services.AddDbContext<JobbyDbContext>(options =>
{
options.UseSqlServer(_mssqlContainer.GetConnectionString());
});

services.EnsureDbCreatedAsync<JobbyDbContext>().ConfigureAwait(true);
});
}
}
public static class ServiceCollectionExtensions
{
public static void RemoveDbContext<T>(this IServiceCollection services) where T : DbContext
{
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(T));
if (descriptor != null)
{
services.Remove(descriptor);
}
}
}
public static class ServiceCollectionExtensions
{
public static void RemoveDbContext<T>(this IServiceCollection services) where T : DbContext
{
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(T));
if (descriptor != null)
{
services.Remove(descriptor);
}
}
}
7 Replies
Pobiega
Pobiega16mo ago
Don't just remove the context, you must also remove the context options That's what actually contains your configured setup
Nickolaki
NickolakiOP16mo ago
Ah damn, I had thought it was something like this. Will give it a go boss. What type is context options? DbContextOptions or something?
Pobiega
Pobiega16mo ago
Yup
Nickolaki
NickolakiOP16mo ago
Cheers boss Will let you know how it goes 🤣🤣
Pobiega
Pobiega16mo ago
It's whatever type your context constructor takes in
Nickolaki
NickolakiOP16mo ago
Working! Thank you bro
public static void RemoveDbContext<T>(this IServiceCollection services) where T : DbContext
{
var dbContextOptionsService = services.SingleOrDefault(
d => d.ServiceType ==
typeof(DbContextOptions<T>));

if (dbContextOptionsService != null)
{
services.Remove(dbContextOptionsService);
}


var dbContextService = services.SingleOrDefault(d => d.ServiceType == typeof(T));
if (dbContextService != null)
{
services.Remove(dbContextService);
}
}
public static void RemoveDbContext<T>(this IServiceCollection services) where T : DbContext
{
var dbContextOptionsService = services.SingleOrDefault(
d => d.ServiceType ==
typeof(DbContextOptions<T>));

if (dbContextOptionsService != null)
{
services.Remove(dbContextOptionsService);
}


var dbContextService = services.SingleOrDefault(d => d.ServiceType == typeof(T));
if (dbContextService != null)
{
services.Remove(dbContextService);
}
}
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server