-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Fix: Cryptic "No constructor" Error When ILoggerFactory Is Not Registered (Issue #1153) #1157
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
Changes from 1 commit
e1f6caa
0b4fa68
e0ae1ac
25ffe81
883d940
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |||||||||||||
| using MediatR.Pipeline; | ||||||||||||||
| using Microsoft.Extensions.DependencyInjection; | ||||||||||||||
| using Microsoft.Extensions.DependencyInjection.Extensions; | ||||||||||||||
| using Microsoft.Extensions.Logging; | ||||||||||||||
|
|
||||||||||||||
| namespace MediatR.Registration; | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -470,8 +471,25 @@ public static void AddRequiredServices(IServiceCollection services, MediatRServi | |||||||||||||
| MediatRServiceCollectionExtensions.LicenseChecked = false; | ||||||||||||||
|
|
||||||||||||||
| services.TryAddSingleton(serviceConfiguration); | ||||||||||||||
| services.TryAddSingleton<LicenseAccessor>(); | ||||||||||||||
| services.TryAddSingleton<LicenseValidator>(); | ||||||||||||||
| services.TryAddSingleton(static sp => | ||||||||||||||
| { | ||||||||||||||
| var loggerFactory = sp.GetService<ILoggerFactory>() | ||||||||||||||
| ?? throw new InvalidOperationException( | ||||||||||||||
| "MediatR requires ILoggerFactory to be registered. " + | ||||||||||||||
| "Call services.AddLogging() before services.AddMediatR()."); | ||||||||||||||
|
Comment on lines
+476
to
+479
|
||||||||||||||
| var config = sp.GetService<MediatRServiceConfiguration>(); | ||||||||||||||
| return config != null | ||||||||||||||
| ? new LicenseAccessor(config, loggerFactory) | ||||||||||||||
| : new LicenseAccessor(loggerFactory); | ||||||||||||||
|
Comment on lines
+480
to
+483
|
||||||||||||||
| var config = sp.GetService<MediatRServiceConfiguration>(); | |
| return config != null | |
| ? new LicenseAccessor(config, loggerFactory) | |
| : new LicenseAccessor(loggerFactory); | |
| var config = sp.GetRequiredService<MediatRServiceConfiguration>(); | |
| return new LicenseAccessor(config, loggerFactory); |
Copilot
AI
Feb 22, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The TryAddSingleton call is missing the type parameter. This will not register LicenseAccessor in the DI container correctly. Change line 474 to: services.TryAddSingleton<LicenseAccessor>(static sp =>
Copilot
AI
Feb 22, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The TryAddSingleton call is missing the type parameter. This will not register LicenseValidator in the DI container correctly. Change line 485 to: services.TryAddSingleton<LicenseValidator>(static sp =>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR description states that this method should be simplified to use GetRequiredService for both licenseAccessor and licenseValidator, removing the fallback logic with
?? new LicenseAccessor(...)and?? new LicenseValidator(...). However, this change was not implemented - only a trailing whitespace was removed from line 69. The fallback logic on lines 65-70 should be replaced with:var licenseAccessor = serviceProvider.GetRequiredService<LicenseAccessor>();andvar licenseValidator = serviceProvider.GetRequiredService<LicenseValidator>();as described in the PR description.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot open a new pull request to apply changes based on this feedback