Home > Backend Development > C++ > How to Resolve Multiple Service Implementations with Custom Keys in ASP.NET Core?

How to Resolve Multiple Service Implementations with Custom Keys in ASP.NET Core?

Patricia Arquette
Release: 2025-01-29 21:16:11
Original
620 people have browsed it

How to Resolve Multiple Service Implementations with Custom Keys in ASP.NET Core?

Use the custom key to resolve multiple services in ASP.NET CORE to achieve

When registering multiple specific implementation of the same interface in ASP.NET CORE, you may encounter definition unique keys to distinguish their restrictions. However, you can use the commission to analyze the appropriate implementation when runtime to solve this problem.

For this, please follow the steps below:

    Definition and sharing commission:
  1. Create a commission called

    , it accepts a key and returns a
      instance.
    • ServiceResolver IService
    Registering specific implementation and mapping keys:
  2. Use to register each specific implementation in the file.

      Add a
    • Inspector registration. In this registration, use the statement to use the AddTransient to map the key to the corresponding specific implementation. Startup.cs
    • ServiceResolver switch Use serverEsolver: GetService
  3. In the class registered through DI, use
  4. to obtain the real implementation. Pass the proper keys as a parameter to the parser.

      Example:
    • ServiceResolver
    Note:
  5. This solution will not solve the problem of the data injection constructor during the registration period. For this reason, please consider exploring other mechanisms, such as
or custom container implementation.

This Revised Response Maintains The Image and ITS Original Format While restes. as it's almedy well-facttted.

The above is the detailed content of How to Resolve Multiple Service Implementations with Custom Keys in ASP.NET Core?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template