❔ ✅ How to debug "More than twenty 'IServiceProvider' instances have been created ... "?
Hi, so I started getting this error after I connected my backend to my frontend. It didn't appear before or I didn't do enough testing to encounter it. I checked my dbcontext setup, program.cs, services, etc and can't spot the mistake... here is the full error if someone can give me a pointer on where to look: https://pastebin.com/aUuK5E7C
Pastebin
fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddle...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
26 Replies
You'll need to share your code, unsuprisingly.
Hey, if i was unclear, i meant that in general, how do you go about solving this and where to look in general.
look at the changes you made before getting the error :)
Well you've made a code error somewhere
we can't give you more info than that
fair 🙂 i was thinking along "look in service, maybe theres a x or y" but thanks anyway
does your DbContext have a constructor?
well there is no way to tell for sure, but the link in the error is the most likely problem
yeah there is a constructor, here is the whole config. also i just learned this whole thing 2 weeks ago so please bear with me 🙂
Why are you injecting
IConfiguration
?
thats very unorthodox
your connectionstring should be fed via the DbContextOptions, which should be generic
public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options)
I was taught that this is the way to go, using _config to get the default connection string in the appsettings.json. im not really sure how to feed it to dbcontext directly. I'm guessing using builder.services in program.cs? is it very significant to do it this way vs what you recommended?
It is the correct way to do it.
I'm not sure if it's related to your problem thou, but you should never be creating contexts manually
I did as you recommended (at least from what I understood) and moved things to program.cs.
and now dbcontext is as such:
I'm not sure if it's necessary to do this line
builder.Services.AddSingleton(npgsqlBuilder);
but I read somewhere that maybe this needs to be a singleton and shouldn't have an instance created every time. anyway the problem still persistsIm not sure why that needs to be a builder at all
normally you'd just do...
services.AddDbContext<MyDbContext>(x => x.UseNpgsql("My Connection String"));
you can ofc replace the connstr with a variable or expression that gets it from the configurationservices as in from the IServicCollection?
also I keep seeing Startup.cs but for some reasons our instructor doesn't use it at all
it used to be part of the boilerplate for .net 5 and before
since .net 6 we've removed it
you still have the same code, just in Program.cs now
yeah, so
builder.Services
for youAh I see, thank you for the info.
I am wondering if there's something wrong with how I used HTTP Context since it's mentioned in the error at the very bottom... Atlhough I don't really understand what it's saying. I only used HTTP Context here
nope that seems fine at a glance
I'd probably return 401 instead of 400 if the user didnt have a nameidentifier claim thou 😛
Haha you're absolutely right, will change it to that. Also I solved it!... by adding this line
dataSource.Dispose();
and data source referring to var dataSource = npgsqlBuilder.Build();
I'm still not sure that builder needs to be a thing
But then again, I have not worked with npgsql in a while
yeah i don't know enough to do it otherwise right now, and I still need to do my front end (hello react my old friend)
so I will leave it alone for now ...
thank you so much for your help!!!
np
if you think we are done here, feel free to $close this thread
Use the /close command to mark a forum thread as answered
thanks, will do
What's the CrudController?
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.