完整的錯誤正文
System.AggregateException: '一些服務無法被構建
(在驗證服務描述符'ServiceType時出錯。WebApp.Application.Common.Interfaces.IUserService
生命期。Transient ImplementationType。WebApp.Application.Common.Services.UserService'。
Unable to resolve service for type 'WebApp.Application.Common.Interfaces.ISettingsService'。
而試圖激活'WebApp.Application.Common.Services.UserService'。)'
我有UserService負責創建用戶(DB模型User),我需要從SettingsService(DB模型NotificationSettings、PersonalSettings)呼叫AddSettings,為新注冊的用戶創建設定物件。目前,我正試圖通過UserService的AddUser方法來做這件事:
public void AddUser(AddUserDTO addUserDTO)。
{
try
{
userRepository.AddAsync(mapper.Map<AddUserDTO, User> (addUserDTO))。
var user = userRepository.FindAsync(a => a.FullName == addUserDTO.FullName && a.Email == addUserDTO.Email) 。
settingsService.AddSettings(user.Id)。
}
catch (Exception ex)
{
logger.LogError(ex.Message)。
throw;
}
AddSettings方法:
public async void AddSettings(int Id)
{
try
{
notifySettingsRepository.Add(new NotificationSettings
{
UserId = id,
PushNewFollowerNotify = false,
PushNewMessageNotify = false,
PushLikeNotify = false, PushLikeNotify = false.
});
personalSettingsRepository.Add(new PersonalSettings
{
UserId = id,
FollowerSeeFollowers = false,
FollowerSeeSavedRecipe = false,
});
}
catch (Exception ex)
{
logger.LogError(ex.Message)。
}
}
UserService建構式:
private readonlyIUserRepository userRepository;
private readonly ISettingsService settingsService;
private readonly ILogger< UserService> logger;
private readonly IMapper mapper。
public UserService(IUserRepository userRepository, ISettingsService settingsService, ILogger<UserService> logger, IMapper mapper)。
{
this.userRepository = userRepository;
this.settingsService = settingsService;
this.logger = logger;
this.mapper = mapper。
而且這兩個服務都在Startup.cs中注冊了
services.AddTransient<ISearchService, SearchService>()。
services.AddTransient<IUserService, UserService>()。
我相信問題在于我如何將SettingsService注入到UserService中,但我找不到任何有關的資訊。
uj5u.com熱心網友回復:
你應該注冊ISettingsService服務:
services.AddTransient<ISettingsService, SettingsService>()。
在你的例子中,你應該注冊一個實作ISettingsService介面的類,就像UserRepository類實作IUserRepository介面一樣
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/306660.html
標籤:
下一篇:如何用C#向csv檔案添加標題?
