Skip to content

Conversation

@A-Ebrahimzadeh-dev
Copy link

Summary

This PR adds support for registering services with a service key in Scrutor.
Currently, Scrutor does not provide a way to register keyed services.
With this change, developers can specify a key when registering a service via ServiceDescriptorAttribute.

Changes

  • Modified ServiceDescriptorAttribute to allow an optional ServiceKey property.
  • Updated registration logic to respect ServiceKey if provided.
  • Preserved backward compatibility for existing code without keys.

Motivation

Adding keyed services allows more flexible dependency injection scenarios,
such as having multiple implementations of the same interface and selecting them by key at resolution time.

Example

[ServiceDescriptor(serviceType: typeof(IMyService), lifetime: ServiceLifetime.Transient, serviceKey: "specificService")]
public class MyService : IMyService { }

@khellang
Copy link
Owner

khellang commented Sep 11, 2025

Hi @A-Ebrahimzadeh-dev! 👋🏻

This looks like a good contribution, thank you! 🙏🏻

Would you be able to add some tests?

@A-Ebrahimzadeh-dev
Copy link
Author

Hi @A-Ebrahimzadeh-dev! 👋🏻

This looks like a good contribution, thank you! 🙏🏻

Would you be able to add some tests?

Hi 👋🏻 Thanks a lot for the feedback! I’ve added the tests as requested. Please let me know if you’d like me to improve or adjust anything 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants