C
C#•9mo ago
only you know

System.AggregateException

'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: BaseApi.Repository.UserRepository Lifetime: Singleton ImplementationType: BaseApi.Repository.UserRepository': Unable to resolve service for type 'MongoDB.Driver.IMongoDatabase' while attempting to activate 'BaseApi.Repository.UserRepository'.)'
'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: BaseApi.Repository.UserRepository Lifetime: Singleton ImplementationType: BaseApi.Repository.UserRepository': Unable to resolve service for type 'MongoDB.Driver.IMongoDatabase' while attempting to activate 'BaseApi.Repository.UserRepository'.)'
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
No description
No description
No description
No description
No description
No description
No description
No description
No description
No description
129 Replies
Pobiega
Pobiega•9mo ago
Your UserRepository class request a IMongoDatabase from the service provider but you have never registered any IMongoDatabase type
only you know
only you knowOP•9mo ago
so what should I do
Pobiega
Pobiega•9mo ago
you must register IMongoDatabase somehow. Where is that interface from?
only you know
only you knowOP•9mo ago
it has his own interface comes with mongodb.driver
only you know
only you knowOP•9mo ago
No description
Pobiega
Pobiega•9mo ago
okay, and how does the documentation suggest you get an instance of this? ah I see, thats whats happening in your MongoDb class
only you know
only you knowOP•9mo ago
what about this it might be wrong
No description
Pobiega
Pobiega•9mo ago
that is the class you should be requesting, not IMongoDatabase, and to do that you must register MongoDb
only you know
only you knowOP•9mo ago
then I should change here right
No description
Pobiega
Pobiega•9mo ago
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
only you know
only you knowOP•9mo ago
that code meaning constructor or whole repo class?
Pobiega
Pobiega•9mo ago
that constructor
only you know
only you knowOP•9mo ago
I should use MongoDB class instead of IMongoDatabase right
Pobiega
Pobiega•9mo ago
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 trouble
only you know
only you knowOP•9mo ago
I should make it scope so I gotta call MongoDB directly because in that class I already created new client
Pobiega
Pobiega•9mo ago
do you know the difference between singleton, scoped and transient?
only you know
only you knowOP•9mo ago
yes I think
Pobiega
Pobiega•9mo ago
kinda? I'm not sure what "call" and "directly" are doing in that sentence thou you dont call classes
only you know
only you knowOP•9mo ago
some english problems sry about that
only you know
only you knowOP•9mo ago
so what should I do now
No description
only you know
only you knowOP•9mo ago
I changed the MongoDB class as MongoDBModel
Pobiega
Pobiega•9mo ago
have you registered it with your service collection? also, you can remove the first two lines in the constructor, and the second parameter
only you know
only you knowOP•9mo ago
you mean this?
No description
only you know
only you knowOP•9mo ago
okay
Pobiega
Pobiega•9mo ago
yes builder.Services is your service collection. It needs to contain everything that gets resolved in your application
only you know
only you knowOP•9mo ago
is it still missing parts? it contains my repo and mongodbsettings and I have the connection string at appsettings.json
Pobiega
Pobiega•9mo ago
it doesnt contain MonogDbModel
only you know
only you knowOP•9mo ago
should I add db model as scope?
Pobiega
Pobiega•9mo ago
yes.
only you know
only you knowOP•9mo ago
I guess there are still missing somethings.
System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: BaseApi.Repository.UserRepository Lifetime: Scoped ImplementationType: BaseApi.Repository.UserRepository': Unable to resolve service for type 'System.String' while attempting to activate 'BaseApi.MongoDB.MongoDBModel'.) (Error while validating the service descriptor 'ServiceType: BaseApi.MongoDB.MongoDBModel Lifetime: Scoped ImplementationType: BaseApi.MongoDB.MongoDBModel': Unable to resolve service for type 'System.String' while attempting to activate 'BaseApi.MongoDB.MongoDBModel'.)'
System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: BaseApi.Repository.UserRepository Lifetime: Scoped ImplementationType: BaseApi.Repository.UserRepository': Unable to resolve service for type 'System.String' while attempting to activate 'BaseApi.MongoDB.MongoDBModel'.) (Error while validating the service descriptor 'ServiceType: BaseApi.MongoDB.MongoDBModel Lifetime: Scoped ImplementationType: BaseApi.MongoDB.MongoDBModel': Unable to resolve service for type 'System.String' while attempting to activate 'BaseApi.MongoDB.MongoDBModel'.)'
Pobiega
Pobiega•9mo ago
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
only you know
only you knowOP•9mo ago
oh I should change the constructor like this
No description
Pobiega
Pobiega•9mo ago
are you serious?
only you know
only you knowOP•9mo ago
wdym
Pobiega
Pobiega•9mo ago
No description
Pobiega
Pobiega•9mo ago
????
only you know
only you knowOP•9mo ago
I thought its throwing error because of string type
Pobiega
Pobiega•9mo ago
Stop. Close this project.
only you know
only you knowOP•9mo ago
why
Pobiega
Pobiega•9mo ago
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
only you know
only you knowOP•9mo ago
okay. thank you
only you know
only you knowOP•9mo ago
@Pobiega what about now?
No description
Pobiega
Pobiega•9mo ago
there we go
only you know
only you knowOP•9mo ago
I also added the services like this
No description
Pobiega
Pobiega•9mo ago
looks good
only you know
only you knowOP•9mo ago
server works fine but something wrong when I send requests I mean app runs fine.
only you know
only you knowOP•9mo ago
Pobiega
Pobiega•9mo ago
look at the constructor for UserController
only you know
only you knowOP•9mo ago
Is this about private readonly?
No description
Pobiega
Pobiega•9mo ago
its not it about UserService look at your registrations again what did you register, and what are you injecting?
only you know
only you knowOP•9mo ago
oh I see so I should inject IUserService?
Pobiega
Pobiega•9mo ago
yes or register the UserServic itself and inject it not a mix of both
only you know
only you knowOP•9mo ago
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?
Pobiega
Pobiega•9mo ago
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
only you know
only you knowOP•9mo ago
so for this project interface is useless?
Pobiega
Pobiega•9mo ago
based on what you've shown, yes an interface with a single implementing class serves no real purpose
only you know
only you knowOP•9mo ago
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.
Pobiega
Pobiega•9mo ago
then wrap the repository in an interface the services are not directly connected to the database they go via the repository
only you know
only you knowOP•9mo ago
oh now I got it. and I have 2 interfaces already contains same methods. thats why IUserService is useless rn
Pobiega
Pobiega•9mo ago
when using repo+service, normally you put all the database interactions in the repository, and the business logic goes in the service
only you know
only you knowOP•9mo ago
thats true I removed interface of userservice and injected from userrepository
only you know
only you knowOP•9mo ago
Do I need mongodbcontext class for fill these because connection string returns empty
No description
only you know
only you knowOP•9mo ago
or are there another ways
Pobiega
Pobiega•9mo ago
? if your connection strings are empty, you are not resolving them correctly
only you know
only you knowOP•9mo ago
my connection is in appsettings only am I resolving the appsettings wrong?
Pobiega
Pobiega•9mo ago
show me your MongoDbSettings class
only you know
only you knowOP•9mo ago
No description
only you know
only you knowOP•9mo ago
also my docker-compose file
No description
Pobiega
Pobiega•9mo ago
your environment variable name doesnt match the appsettings names but that doesnt explain why the values in the appsettings isnt being loaded
only you know
only you knowOP•9mo ago
I fixed now, didn't realized that should load it from appsettings? don't have any idea rn
Pobiega
Pobiega•9mo ago
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 setters
only you know
only you knowOP•9mo ago
so in appsettings my connection string is missing
Pobiega
Pobiega•9mo ago
no?
Pobiega
Pobiega•9mo ago
No description
Pobiega
Pobiega•9mo ago
looks fine to me
only you know
only you knowOP•9mo ago
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
Pobiega
Pobiega•9mo ago
no
only you know
only you knowOP•9mo ago
are there any problem here? it's my repository and I think it can't find the collection
No description
Pobiega
Pobiega•9mo ago
it says _database is unused that is a problem
only you know
only you knowOP•9mo ago
do I need _database? I can reach the database but not collection
No description
only you know
only you knowOP•9mo ago
it might be useless here
Pobiega
Pobiega•9mo ago
where is _collection getting its value from?
only you know
only you knowOP•9mo ago
it has to be from db so you mean the problem because from _database's unuse
Pobiega
Pobiega•9mo ago
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 here
only you know
only you knowOP•9mo ago
Isn't it assigned here
No description
only you know
only you knowOP•9mo ago
don't get mad pls
Pobiega
Pobiega•9mo ago
its really hard
only you know
only you knowOP•9mo ago
to explain me sth right
Pobiega
Pobiega•9mo ago
Why are you making APIs when you dont understand the very basics of programming?
Pobiega
Pobiega•9mo ago
No description
Pobiega
Pobiega•9mo ago
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
only you know
only you knowOP•9mo ago
😕
Pobiega
Pobiega•9mo ago
see these are not complicated problems, not really but if you lack understanding for the fundamentals of object oriented programming, it becomes near impossible
only you know
only you knowOP•9mo ago
it's just you're kinda harsh to me
Pobiega
Pobiega•9mo ago
oh so its my fault you dont understand the basics?
only you know
only you knowOP•9mo ago
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
Pobiega
Pobiega•9mo ago
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
only you know
only you knowOP•9mo ago
got it thank you for suggestions captain can I ask last thing about this project before you kill me?
Pobiega
Pobiega•9mo ago
sure
only you know
only you knowOP•9mo ago
only you know
only you knowOP•9mo ago
is this about connection string?
Pobiega
Pobiega•9mo ago
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
only you know
only you knowOP•9mo ago
btw I didn't compose up when do I need to do it
Pobiega
Pobiega•9mo ago
I wouldn't be using docker compose during development to me, thats more for deployment
only you know
only you knowOP•9mo ago
probably because I created whole database stuff from docker and shell
Pobiega
Pobiega•9mo ago
open up your docker and check how the db is running
only you know
only you knowOP•9mo ago
it's working btw status:running
Pobiega
Pobiega•9mo ago
No description
Pobiega
Pobiega•9mo ago
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 names
only you know
only you knowOP•9mo ago
I have the yellow one
Pobiega
Pobiega•9mo ago
however, if your program runs OUTSIDE of docker, it can use localhost to communicate with a docker container from inside a composed group show
only you know
only you knowOP•9mo ago
No description
Pobiega
Pobiega•9mo ago
okay, so its not running as part of a group what ports?
only you know
only you knowOP•9mo ago
27017
Pobiega
Pobiega•9mo ago
27017:27017?
only you know
only you knowOP•9mo ago
yes
Pobiega
Pobiega•9mo ago
and that is the port mongo listens on?
only you know
only you knowOP•9mo ago
but port of my-api in docker compose is 5000:80
Pobiega
Pobiega•9mo ago
doesnt matter, you're not running it as part of that use a debugger to verify what connectionstring is being used
only you know
only you knowOP•9mo ago
okay
only you know
only you knowOP•9mo ago
No description
Pobiega
Pobiega•9mo ago
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
only you know
only you knowOP•9mo ago
mongodb://localhost:27017/<database> is this the one that I need?
Pobiega
Pobiega•9mo ago
mongodb://user:password@localhost:27017
only you know
only you knowOP•9mo ago
thank you everything works fine but i'll start from beginning also reread the codes to learn
Pobiega
Pobiega•9mo ago
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
only you know
only you knowOP•9mo ago
so I gotta move part to part
Pobiega
Pobiega•9mo ago
yeah learn each part on its own, then start adding them together
only you know
only you knowOP•9mo ago
okay thank you so much
Want results from more Discord servers?
Add your server