c# - 添加依赖注入会在 .NET Core 中产生异常
问题描述
我收到以下异常。为什么会这样,我该如何解决?
当SMSService.cs
我评论构造函数时,它开始工作,但我需要ISMSSender
从它访问。
System.AggregateException:'某些服务无法构造(验证服务描述符时出错'ServiceType:Authentication.API.Repository.Interface.ISMSService Lifetime:Singleton ImplementationType:Authentication.API.Repository.SMSService':无法解析服务尝试激活“Authentication.API.Repository.SMSService”时输入“SMSSender.API.Repository.Interface.ISMSSender”。)
Authentication.API.Repository.SMSService
public class SMSService : ISMSService
{
private readonly ISMSSender _smsSender;
public SMSService(ISMSSender smsSender)
{
_smsSender = smsSender;
}
}
启动 ...
services.AddTransient<ISMSService, SMSService>();
...
SMSSender.API.Repository.Interface.ISMSSender
public interface ISMSSender
{
Task<SMSUser> SendSMS(SMSUser prospectiveUser);
}
SMSSender.API.Repository.Interface.SMSSender
public class SMSSender : ISMSSender
{
public async Task<SMSUser> SendSMS(SMSUser sMSUser)
{ }
}
解决方案
看起来你没有注册 SMSSender(你做了服务):
services.AddTransient<ISMSService, SMSService>();
services.AddTransient<ISMSSender, SMSSender>();
推荐阅读
- abap - S/4 HANA 中的 ABAP CDS 使用计算视图引擎?
- asp.net-core - Azure AD 身份验证后立即运行自定义方法
- google-chrome - 在 Chrome 中找不到 VueJS 本地
- laravel - 无法检查用户是否有权限
- sql - 如何返回从存储在多个记录中的值构建的单个值?
- flutter - 如何删除默认导航路线动画
- java - Java SE:泛型和继承/多态
- vb.net - 使用 VB.NET 拆分文本文件到 (x) 个文件的路径
- r - 如何将 group_by 和 summise 函数与条件一起使用?
- python - 如何使用 GeoPandas 绘制多个图层