Keyed Dependency Injection (DI) in dotnet core 8.0 | Register and retrieve DI using Keys

Поделиться
HTML-код
  • Опубликовано: 6 окт 2024
  • Keyed dependency injection (DI) services are used to register and retrieve DI services using keys. Using keyed dependency injection you can register multiple classes implementing the same interface in the dotnet core. There are 3 new methods in the Keyed dependency injection.
    🔴 AddKeyedSingleton
    🔴 AddKeyedScoped
    🔴 AddKeyedTransient
    Let's say you have one interface with the name IEmployeeRepository. And two classes EmployeeRepository & TempEmployeeRepository are implementing this same interface. Now if you want to use both of the implementations in your application, then you can register them in DI container and access them in your app.
    To access the instance in the controller you can use the FromKeyedServices attribute.
    Example: [FromKeyedServices("employee")] IEmployeeRepository employeeRepository.
    This concept is applicable to Minimal APIs, MVC, and SignalR Hubs and is available in DotNet Core 8.0
    🚫🚫 Issue related to Keyed Dependency Injection is not working using the constructor 🚫🚫
    🔗 github.com/dot...
    💰 SUPPORT Nitish Kaushik ☕💳 by donating for more quality content!
    📱UPI: nitishkaushik@icici
    💳 Paypal: www.paypal.com...
    ☕ BuyMeACoffee: www.buymeacoff...
    Your support is greatly appreciated! 🙌🎥📚
    👋👋 Join telegram group: t.me/webgentle
    Want to setup the development environment like me:
    🧑‍💻 Laptop for developers: amzn.to/3YkvKCV
    🐀 Silent Mouse: amzn.to/3OmOo8E
    🎤 Noise cancellation Mic: rb.gy/9h89o
    🌟 Screen recording software: techsmith.z6rj...

Комментарии • 2

  • @JtendraShahani
    @JtendraShahani 10 месяцев назад

    Can we use keyed services in front end. Let's say I have a Blazor Server application with a service. How can I inject in a razor component?

  • @amannayyar5124
    @amannayyar5124 Год назад

    We are getting the below error in swagger UI, while we are using DI with Method Injection:
    TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.