簡體   English   中英

ABP 框架 Singleton 服務 DI 在 Swagger 上失敗

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM