Description
Is there an existing issue for this?
- I have searched the existing issues
Is your feature request related to a problem? Please describe the problem.
Since the release of .NET Core and the introduction of Microsoft.Extensions.DependencyInjection, StackOverflow is flooded with questions where the developer reports getting the following exception:
Unable to resolve service for type '...'
While the reason almost always is that the dependency hasn't been added to the IServiceCollection
, it truck me that regularly the exception occurs because the developer didn't add the proper framework dependencies using one of the .AddXXX
overloads. Take a look, for instance, as this recent StackOverflow question. In that case the following message is thrown:
Unable to resolve service for type 'Microsoft.Extensions.Logging.ILoggerFactory' while attempting to activate 'Microsoft.Extensions.Localization.ResourceManagerStringLocalizerFactory'.
While we could respond to these kinds of problems with RTFM, because this is what it basically always comes down to, it doesn't help developers that are starting out with this new technology.
I'm, therefore, hoping that the experience can be improved for starting developers, especially around common framework types.
Describe the solution you'd like
A possible solution is to mark those common framework types, such as the above ILoggerFactory
with an attribute that gives a short explanation on what the typical solution is when a registration for that service type is missing, and a link to some basic documentation showing how to set up that part of the framework. The DI Container could than, append the exception message with that information.
For instance:
namespace Microsoft.Extensions.Logging
{
[ServiceMissingDocumentation(
message: "You might be missing a call to .AddLogger() on your IServiceCollection.",
url: "https://doc.ms/84FDKv9")]
public interface ILoggerFactory : IDisposable
{
...
}
}
This could than result in the following exception message:
Unable to resolve service for type 'Microsoft.Extensions.Logging.ILoggerFactory' while attempting to activate 'Microsoft.Extensions.Localization.ResourceManagerStringLocalizerFactory'. You might be missing a call to .AddLogger() on your IServiceCollection. For more information, go to https://doc.ms/84FDKv9.
Additional context
While I realize that this doesn't solve all issues, and certainly isn't argument for developers to not RTFM, I believe such feature (independent to how you choose to implement it) could really improve the whole experience around DI for developers who are either new to the principles, or are just new to working with MS.DI.