C#
C#

help

Root Question Message

thinker227
thinker2272/5/2023
❔ ✅ AddSingleton and AddDbConnection are not running whatsoever

I have this code
services.AddSingleton<DbConnection>(_ =>
{
    SqliteConnectionStringBuilder stringBuilder = new()
    {
        DataSource = "MemoryDb",
        Mode = SqliteOpenMode.Memory
    };
    SqliteConnection connection = new(stringBuilder.ToString());
    connection.Open();

    return connection;
});

services.AddDbContext<MerpieContext>((serviceProvider, options) =>
{
    var connection = serviceProvider.GetRequiredService<DbConnection>();
    options.UseSqlite(connection, sqliteOptions =>
    {
        sqliteOptions.UseNodaTime();
    });
});

The configuration lambdas are not running. I have no idea why. The DbConnection and MerpieContext are being added to the services, but they're not being configured.
I've tried setting breakpoints inside the lambdas but they're never hit.
Tvde1
Tvde12/5/2023
Is the line services.AddSingleton<DbConnection>(_ => hit?
thinker227
thinker2272/5/2023
Yes.
Tvde1
Tvde12/5/2023
are you really calling this method?
Tvde1
Tvde12/5/2023
alright
Tvde1
Tvde12/5/2023
what kind of an instance do you get if DI provides you with a MerpieContext?
thinker227
thinker2272/5/2023
wdym?
Tvde1
Tvde12/5/2023
do you ever request a MerpieContext
Tvde1
Tvde12/5/2023
in a controller or such
thinker227
thinker2272/5/2023
Yes
private void RunMigrations()
{
    using var scope = Services.CreateScope();
    var context = scope.ServiceProvider.GetRequiredService<MerpieContext>();
    
    context.Database.Migrate();
}
Tvde1
Tvde12/5/2023
and this runs fine?
thinker227
thinker2272/5/2023
Well I get an error on context.Database.Migrate(); that EF can't find the DB file, which I assume is because the connection string is never set.
thinker227
thinker2272/5/2023
But the first two lines run fine.
Tvde1
Tvde12/5/2023
I suppose you're not accidentally adding MerpieContext to the services somewhere else
Tvde1
Tvde12/5/2023
e.g. a previous configuration
Tvde1
Tvde12/5/2023
I'm not sure what happens if a context is added twice
thinker227
thinker2272/5/2023
Before that I'm actually removing all MerpieContext because this is supposed to replace any previous configuration.
Tvde1
Tvde12/5/2023
as a sanity check, what happens if you run it with
services.AddDbContext<MerpieContext>((serviceProvider, options) commented out?
Tvde1
Tvde12/5/2023
hmm
Tvde1
Tvde12/5/2023
And also check whether break points are on. E.g. you're running debug in rider and not just "Run"
thinker227
thinker2272/5/2023
Well other breakpoints are being hit at least
Mayor McCheese
Mayor McCheese2/5/2023
Hmm I think I ran into this issue a couple years back and had run an ensure db created method
Mayor McCheese
Mayor McCheese2/5/2023
Ultimately I switched over to a file but I forget why
thinker227
thinker2272/5/2023
at this point a file would probably be like lightyears easier lol
Mayor McCheese
Mayor McCheese2/5/2023
I generally don’t bother with SQLite tbh unless it’s really throw away. I know I’m a loner there
thinker227
thinker2272/5/2023
This is exactly what I'm trying to do, as seen above
thinker227
thinker2272/5/2023
I'm trying to create a connection, but the configuration is not being run
thinker227
thinker2272/5/2023
Moving the connection stuff outside the configuration lambda does nothing
thinker227
thinker2272/5/2023
Still getting that exception
thinker227
thinker2272/5/2023
The file doesn't exist
thinker227
thinker2272/5/2023
It can't find it
thinker227
thinker2272/5/2023
But it shouldn't even be looking for a file
thinker227
thinker2272/5/2023
So the DB is still not being configured
thinker227
thinker2272/5/2023
BUT WHY
thinker227
thinker2272/5/2023
Like is this about EF or about MSDI?
thinker227
thinker2272/5/2023
Is the DI stuff just not working??
thinker227
thinker2272/5/2023
Is this some arcane bug that literally no one has reported
phaseshift
phaseshift2/5/2023
Suggest: Put only that service registration bit in a service collection in a unit test and verify the behaviour with a minimal example
thinker227
thinker2272/5/2023
Okay. So I put all that into a separate test, ran it... and it works.
thinker227
thinker2272/5/2023
The breakpoints are being hit, the DB context is being resolved, the Migrate() call works.
thinker227
thinker2272/5/2023
So... that's weird.
thinker227
thinker2272/5/2023
Then perhaps the issue is that some service descriptors already exist in the service collection which causes it to not create it through the configuration.
djmurp
djmurp2/5/2023
could you put a breakpoint in the constructor of your context, then look at stacktrace to see where it's being created?
thinker227
thinker2272/5/2023
can try that
djmurp
djmurp2/5/2023
if it's not your lambda above then it must be somewhere else
thinker227
thinker2272/5/2023
Okay so the constructor is being hit twice, which is expected. Once from Program.cs and once from my WebApplicationFactory.
thinker227
thinker2272/5/2023
So I again suspect that there's some service I haven't properly cleared out which causes it to not run the configuration.
thinker227
thinker2272/5/2023
okay I think I have an idea what the problem is
djmurp
djmurp2/5/2023
nice
djmurp
djmurp2/5/2023
Is it because a servicedescriptor<MerpieContext> is added (the factory), but you were clearing out all MerpieContext even though there's none, so it's using the descriptor from Program.cs instead?
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy