MediatR请求处理器验证报错:激活IdentityService时无法解析JwtService的问题求助
Let's break down what's going wrong here and get this sorted out step by step.
The Root Cause
Your error message spells out the exact issue:
System.InvalidOperationException: Unable to resolve service for type 'TechneTravel.Infrastructure.Services.JwtService' while attempting to activate 'Newproject.Infrastructure.Identity.IdentityService'
This means your IdentityService is directly depending on the concrete JwtService class in its constructor, not the IJwtService interface you defined in the Application layer. Even though you registered the interface-to-implementation mapping (IJwtService → JwtService), you never registered the concrete JwtService itself—and the DI container can't resolve a type that hasn't been explicitly registered.
Step-by-Step Fixes
1. Update IdentityService to Depend on the Interface
Open your IdentityService implementation in the Infrastructure layer and adjust its constructor to use the IJwtService interface instead of the concrete class:
public class IdentityService : IIdentityService { // Replace concrete JwtService with the IJwtService interface private readonly IJwtService _jwtService; // Update constructor parameter to match the interface public IdentityService(IJwtService jwtService) { _jwtService = jwtService; } // Rest of your IdentityService implementation... }
This aligns with your existing DI registration (services.AddTransient<IJwtService,JwtService>()), so the DI container can now resolve the dependency correctly.
2. Simplify MediatR Registration
You don't need those three manual registrations for LoginQueryHandler—the services.AddMediatR(Assembly.GetExecutingAssembly()) call you already have is sufficient, as long as:
- Your
LoginQueryHandlerandLoginViewModellive in the same assembly where you're callingAddMediatR(the Application layer). - If you're registering MediatR from the Web layer, explicitly target the Application layer's assembly to avoid missing handlers:
services.AddMediatR(typeof(LoginViewModel).Assembly);
MediatR's AddMediatR method automatically scans the provided assembly for all IRequestHandler implementations and registers them, so manual registrations are redundant here.
3. Confirm DI Registration Order
Make sure you register your Infrastructure layer services before registering MediatR. For example, in your Program.cs (or Startup.cs):
// First register Infrastructure services (includes IJwtService and IIdentityService) services.AddInfrastructureServices(); // Then register MediatR, which depends on those services services.AddMediatR(typeof(LoginViewModel).Assembly);
This ensures that when MediatR tries to instantiate LoginQueryHandler, the required dependencies (IIdentityService and IJwtService) are already available in the DI container.
Final Checks
After making these changes:
- Double-check that all service dependencies in your layers use interfaces instead of concrete classes (a core rule of layered architecture!).
- Confirm your
LoginQueryHandler's constructor is still correctly injectingIIdentityServiceandIJwtService(your existing code here looks good).
Once you apply these fixes, the DI container should resolve all dependencies properly, and your MediatR handler will run without errors.
内容的提问来源于stack exchange,提问作者Abdulgany Mohamed

