![](/img/trans.png)
[英].Net 5 Console Background service exception with DI of Entity Framework Core
[英]ABP Framework Singleton Service DI fails on Swagger
我正在尝试按照文档将我的服务作为 Singleton 注入到我的单层应用程序中。
我的界面:
public interface ILObjectAppService: IApplicationService
{
DateTime GetTime();
string Login(string userName, string password, int frmNo);
void Disconnect();
}
实施:
[Dependency(ServiceLifetime.Singleton, TryRegister = true)]
[ExposeServices(typeof(ILObjectAppService))]
public class LObjectAppService : ApplicationService, ILObjectAppService
{
public static UnityObjects.UnityApplication _unityApp;
private readonly IStatusAppService _statusAppService;
public DateTime Time { get; set; }
public LObjectAppService(IStatusAppService statusAppService)
{
_statusAppService = statusAppService;
Time= DateTime.Now;
_unityApp= new UnityObjects.UnityApplication();
}
public DateTime GetTime()
{
return Time;
}
/ ... other methods
}
当我在表示层上使用它时,它工作得很好。
索引.cshtml.cs
public class IndexModel : AbpPageModel
{
private readonly ILObjectAppService _lObjectAppService;
public DateTime LObjectTime { get; set; }
public IndexModel(ILObjectAppService lObjectAppService)
{
_lObjectAppService = lObjectAppService;
}
public async Task OnGetAsync()
{
LObjectTime = _lObjectAppService.GetTime();
}
}
问题是当我导航到 swagger (https://localhost:44337/) 并尝试调用 LObjectAppService 的GetTime
端点时,出现错误RemoteServiceErrorInfo 500
。
日志文件说:
Autofac.Core.Registration.ComponentNotRegisteredException:请求的服务“MyProject.Entegrator.Services.LObject.LObjectAppService”尚未注册。 要避免此异常,请注册组件以提供服务,使用 IsRegistered() 检查服务注册,或使用 ResolveOptional() 方法解析可选依赖项。
我试图在 EntegratorModule.ConfigureServices() 中注册 context.Services.Singleton 但这只会使应用程序崩溃。
我注册我的 singleton 服务是否正确? 这里的最佳做法是什么?
将ApplicationService
注册为 singleton 是不可取的,因为你最终会得到强制依赖。
话虽如此,错误似乎是 DI 容器正在尝试解析具体类型...您使用的是传统的 controller 注册,还是手动连接了 controller? 如果是这样,我建议您可能已经添加了对具体类型LObjectAppService
而不是接口ILObjectAppService
的依赖。
我相信您需要启用[Dependency(ReplaceServices=true)]
来替换现有的注册。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.