似乎IAppSettings建構式中的 IoC 尚未準備好實作。
在詳細介紹之前,我已經閱讀了類似的問題:
我擁有的
我所有的代碼都在 repo 中:

什么不起作用
當您嘗試
IAppSettings在建構式中使用某些應用程式設定值獲取和初始化其他內容時 - 無論是在子類還是基類中,IAppSettings都將無法從 IoC 獲取實作,并導致 NULL 參考例外:public abstract class ServiceBase : Service { public IAppSettings AppSettings { get; set; } public ServiceBase() { // AppSettings would be NULL var test = AppSettings.Get<string>("custom"); } }要么
public class HelloService : ServiceBase { public HelloService() { // AppSettings would be NULL var test = AppSettings.Get<string>("custom"); } }
uj5u.com熱心網友回復:
您不能在建構式中使用任何屬性依賴項,因為只能在創建類并運行建構式之后注入屬性。
您只能通過使用建構式注入在建構式中訪問它,例如:
public class HelloService : ServiceBase { public HelloService(IAppSettings appSettings) { var test = appSettings.Get<string>("custom"); } }或者通過單例訪問依賴:
public class HelloService : ServiceBase { public HelloService() { var test = HostContext.AppSettings.Get<string>("custom"); } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/448351.html標籤:C# asp.net-mvc 服务栈 服务栈-ioc
