System.AggregateException
I'm trying to build an API with using MongoDB. Sorry for so much screenshots I just wanted to show you all my work and understand what to change and what are my mistakes. I'm kinda new at .net core
129 Replies
Your
UserRepository
class request a IMongoDatabase
from the service provider
but you have never registered any IMongoDatabase
typeso what should I do
you must register IMongoDatabase somehow. Where is that interface from?
it has his own interface
comes with mongodb.driver
okay, and how does the documentation suggest you get an instance of this?
ah I see, thats whats happening in your
MongoDb
classwhat about this it might be wrong
that is the class you should be requesting, not
IMongoDatabase
, and to do that you must register MongoDb
then I should change here right
Do you understand how dependency injection works?
yes
like, that code makes no sense at all tbh
you're injecting a database connection, but also creating a new one?
and the one you created is then ignored
that code meaning constructor or whole repo class?
that constructor
I should use MongoDB class instead of IMongoDatabase right
the first two lines create a new
IMongoDatabase
instance, but you are injecting one. And locally creating the connection inside your repo is almost guaranteed to be a very bad idea
also, your repo is a singleton
a singleton with a database connection.... sounds like a recipe for troubleI should make it scope
so I gotta call MongoDB directly because in that class I already created new client
do you know the difference between singleton, scoped and transient?
yes I think
kinda? I'm not sure what "call" and "directly" are doing in that sentence thou
you dont call classes
some english problems sry about that
so what should I do now
I changed the MongoDB class as MongoDBModel
have you registered it with your service collection?
also, you can remove the first two lines in the constructor, and the second parameter
you mean this?
okay
yes
builder.Services
is your service collection. It needs to contain everything that gets resolved in your applicationis it still missing parts? it contains my repo and mongodbsettings and I have the connection string at appsettings.json
it doesnt contain
MonogDbModel
should I add db model as scope?
yes.
I guess there are still missing somethings.
BaseApi.Repository.UserRepository': Unable to resolve service for type 'System.String' while attempting to activate 'BaseApi.MongoDB.MongoDBModel'the constructor for your MongoDbModel takes in two strings change that to take in the db connection options and use those values instead
oh I should change the constructor like this
are you serious?
wdym
????
I thought its throwing error because of string type
Stop. Close this project.
why
https://www.youtube.com/watch?v=USBoZtGt0QU
this is 9 minutes long. go watch it
You clearly do not understand even the basics of dependency injection
so your first step must be to learn that
okay. thank you
@Pobiega what about now?
there we go
I also added the services like this
looks good
server works fine but something wrong when I send requests
I mean app runs fine.
look at the constructor for
UserController
Is this about private readonly?
its not
it about
UserService
look at your registrations again
what did you register, and what are you injecting?oh I see so I should inject IUserService?
yes
or register the UserServic itself and inject it
not a mix of both
I think I should register Userservice
and also inject userservice not IUserService
because I want to use UserService's defined methods. Am I thinking wrong?
or the quite opposite?
both are fine
the interface way is better if you have multiple versions of the service, but I dont think you do
at that point, just remove the interface
so for this project interface is useless?
based on what you've shown, yes
an interface with a single implementing class serves no real purpose
but what I want to do is make my project less dependent to mongodb
I'll make another classes and I'll use my interfaces there too.
then wrap the repository in an interface
the services are not directly connected to the database
they go via the repository
oh now I got it. and I have 2 interfaces already contains same methods.
thats why IUserService is useless rn
when using repo+service, normally you put all the database interactions in the repository, and the business logic goes in the service
thats true
I removed interface of userservice and injected from userrepository
Do I need mongodbcontext class for fill these because connection string returns empty
or are there another ways
?
if your connection strings are empty, you are not resolving them correctly
my connection is in appsettings only am I resolving the appsettings wrong?
show me your
MongoDbSettings
classalso my docker-compose file
your environment variable name doesnt match the appsettings names
but that doesnt explain why the values in the appsettings isnt being loaded
I fixed now, didn't realized that
should load it from appsettings? don't have any idea rn
well yes, based on what I can see you've done that part right
Configure
should load it properly, your names match up, the properties have settersso in appsettings my connection string is missing
no?
looks fine to me
I meant Program.cs I'm sorry
I'll double check it rn
all the names are matching correctly
is it about docker compose?
file
no
are there any problem here? it's my repository and I think it can't find the collection
it says
_database
is unused
that is a problemdo I need _database? I can reach the database but not collection
it might be useless here
where is
_collection
getting its value from?it has to be from db so you mean the problem because from _database's unuse
no i mean, from that screenshot, it appears you never assign to
_collection
meaning it is null
meaning you should be getting a ton of errors hereIsn't it assigned here
don't get mad pls
its really hard
to explain me sth right
Why are you making APIs when you dont understand the very basics of programming?
THIS VARIABLE
never ever gets a value
it has no value
you are never assigning to this, but this is the value you are using in your repository
your repository is NOT using
_database.Users
, which would have worked
It seems very much like you are a beginner trying to make a much too advanced program using a tutorial😕
see these are not complicated problems, not really
but if you lack understanding for the fundamentals of object oriented programming, it becomes near impossible
it's just you're kinda harsh to me
oh so its my fault you dont understand the basics?
I didn't say that
see you're aggresive and I didn't say anything to blame you. I know that I'm beginner
ofc there nothing to blame you I'm just saying
So go make beginner level programs
dont try to make a web api with a database connection and using repositories and services and object lifetimes
learn the basics before moving on to the harder stuff
its absolutely vital to understand object references, object state and accessing other objects state/members
without very strong grasp of those concepts, you end up in a mess like this
got it thank you for suggestions captain
can I ask last thing about this project before you kill me?
sure
is this about connection string?
could be
its an error when mongo is trying to connect to something
so its quite likely that something in your connectionstring is wrong
verify if its using the one from appsettings or the one from your compose file
because if its running in compose, you can't access it via
localhost
btw I didn't compose up when do I need to do it
I wouldn't be using docker compose during development
to me, thats more for deployment
probably because I created whole database stuff from docker and shell
open up your docker and check how the db is running
it's working btw status:running
if you have an icon like the red box, it means its running in a composed group
the yellow box is a child of the red box and cant internally communicate via
localhost
, ie a connection from one child to another can't use localhost
they need to use the service namesI have the yellow one
however, if your program runs OUTSIDE of docker, it can use
localhost
to communicate with a docker container from inside a composed group
showokay, so its not running as part of a group
what ports?
27017
27017:27017
?yes
and that is the port mongo listens on?
but port of my-api in docker compose is 5000:80
doesnt matter, you're not running it as part of that
use a debugger to verify what connectionstring is being used
okay
thats the wrong one
that will only work if your API is running as part of the compose group
it needs to be
localhost:27017
mongodb://localhost:27017/<database> is this the one that I need?
mongodb://user:password@localhost:27017
thank you everything works fine but i'll start from beginning also reread the codes to learn
when learning something new, its often a good idea to cut out the other parts. for example, to learn mongodb, start with a console app, with a hardcoded connectionstring
dont worry about docker, or appsettings, or dependency injection, or ASP
just make a super simple console app that connects to a db, inserts a value, reads a value, done
when you have that working, you can move on
so I gotta move part to part
yeah
learn each part on its own, then start adding them together
okay thank you so much