-
Notifications
You must be signed in to change notification settings - Fork 5.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More examples of Singleton, Transient and Scoped Injection Dependency #41082
Comments
Show me what you're trying to do? If you could provide the code that you have, I might be able to help. |
Hello again! I am facing a runtime issue when trying to instantiate an
implementation for the 'API.Application.Interfaces.IContatoAppService'
interface. The specific error is as follows:
System.ArgumentException: 'Cannot instantiate implementation type
'API.Application.Interfaces.IContatoAppService' for service type
'API.Application.Interfaces.IContatoAppService'.' In the Builder I called:
builder.Services.AddScoped<IContatoAppService, IContatoAppService>() and I
tried other ways too: they all threw an exception.
I'm finding it difficult to solve this problem, as it's not clear where
exactly the error is (Sorry for the inconvenience, but I've got a project
in progress, and from what I know, dependency injection works with Scoped,
Singleton and others - but there is another way, which I couldn't identify,
but which generates the error ) It would be nice if you could provide more
detailed error messages or suggestions about what could be causing this
problem, as the error is pointed out in the builder (very generic). Thank
you in advance for your assistance!
Em qua., 29 de mai. de 2024 às 16:24, David Pine ***@***.***>
escreveu:
… Show me what you're trying to do? If you could provide the code that you
have, I might be able to help.
—
Reply to this email directly, view it on GitHub
<#41082 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AQ7OS2J6SBPMZV2FREP2MCLZEYTP7AVCNFSM6AAAAABIKCHCPCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMZYGEYTANBRG4>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Got it, thank you for the added context, that's very helpful! Ok, I see the issue. When you call: builder.Services.AddScoped<IContatoAppService, IContatoAppService>(); That's registering a scoped Consider this: builder.Services.AddScoped<IContatoAppService, DefaultContatoAppService>(); In this example, the implemenation is the public class DefaultContatoAppService : IContatoAppService
{
// TODO: Implement members.
} Does that make more sense now? Then for unit tests, you could mock the interface and use that. |
Thank you for responding.
I think you'd better see my project to get an idea. I put AddScoped on (and
tried the others) but it was the same thing as nothing. I don't know...
I've been trying for weeks. It might help me know where I'm going wrong!
https://github.com/zabpetrus/bazartemtudo/tree/main/BazarTemTudo
Actually... trying to make dependency injection work. Until now, I'm forced
to use new()...
Thanks!
Em qua., 29 de mai. de 2024 às 20:09, David Pine ***@***.***>
escreveu:
… Got it, thank you for the added context, that's very helpful! Ok, I see
the issue. When you call:
builder.Services.AddScoped<IContatoAppService, IContatoAppService>();
That's registering a scoped IContatoAppService type with a corresponding
implementation of the interface. That's not going to work, since it cannot
instantiate an interface, nothing can. Instead, you need to register (or
map) the corresponding implementation.
Consider this:
builder.Services.AddScoped<IContatoAppService, DefaultContatoAppService>();
In this example, the implemenation is the DefaultContatoAppService. It
has to be defined somewhere:
public class DefaultContatoAppService : IContatoAppService{
// TODO: Implement members.}
Does that make more sense now? Then for unit tests, you could mock the
interface and use that.
—
Reply to this email directly, view it on GitHub
<#41082 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AQ7OS2NGGTQK724LKJTOWGLZEZNZBAVCNFSM6AAAAABIKCHCPCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMZYGM4TMNZQGU>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Type of issue
Thank you
Description
I need to use two types of constructors: one for the application and another for the tester. So, I need to master the subject of dependency injection to avoid the aggregation error, which indicates circular references. I tried to implement the example given, but it didn't work. And there's just one example... What changes should be made to the classes to be able to do dependency injection? What should be added? what should be removed? Help me, because I'm using Moq and I can't remove the constructor with injection! I could give two examples: normal mode and using transient, scoped and singleton and teach how the student can migrate the modes to make it work. Thanks
Page URL
https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection
Content source URL
https://github.com/dotnet/docs/blob/main/docs/core/extensions/dependency-injection.md
Document Version Independent Id
cc5f00d0-fb50-1229-8fa2-e66b1f45b284
Article author
@IEvangelist
Metadata
The text was updated successfully, but these errors were encountered: