2017-07-26 139 views
0

我有我的「appsettings.json」文件內容ASP.Net DI使用類控制器內沒有實例化類控制器內

{ 
"Logging": { 
"IncludeScopes": false, 
"LogLevel": { 
"Default": "Debug", 
"System": "Information", 
"Microsoft": "Information" 
} 
}, 
"appData": { 
"applicationDeveloper": "El Bayames" 
} 
} 

以及從AppSettings的閱讀,更

public class learningDIGlobalVariables 
{ 
    private String _applicationDeveloper; 
    private String _webRootFolderPath; 

    public String applicationDeveloper 
    { 
     get 
     { 
      return _applicationDeveloper; 
     } 
    } 
    public String webRootFolderPath 
    { 
     get 
     { 
      return _webRootFolderPath; 
     } 
    } 

    public learningDIGlobalVariables(IConfigurationRoot auxConfRoot, Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnvironment) 
    { 
     _applicationDeveloper = auxConfRoot["appData:applicationDeveloper"]; 
     _webRootFolderPath = hostingEnvironment.WebRootPath; 
    } 
} 

而設置的容器內,我有

public void ConfigureServices(IServiceCollection services) 
    { 
     // Add framework services. 
     services.AddMvc(); 
     services.AddSingleton<IConfigurationRoot>(Configuration); 
    } 

內的控制器我有

public class HomeController : Controller 
{ 
    private learningDIGlobalVariables _learningDIGlobalVariables; 
    public HomeController(IConfigurationRoot auxConfRoot, Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnvironment) 
    { 
     _learningDIGlobalVariables = new learningDIGlobalVariables(auxConfRoot, hostingEnvironment); 
    } 
} 

如何在控制器內使用該類「learningDIGlobalVariables」而不實例化它?這個類沒有被框架實例化嗎?如果我必須將該類添加到服務中,我該怎麼做並在後者上使用它?

回答

0

如果你不想實例化CALSS比你可以使靜態,去除構造,並添加SetGlobals方法:

public class learningDIGlobalVariables 
{ 
    public static SetGlobals(IConfigurationRoot auxConfRoot, Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnvironment) 
    { 
     _applicationDeveloper = auxConfRoot["appData:applicationDeveloper"]; 
     _webRootFolderPath = hostingEnvironment.WebRootPath; 
    } 
} 

在啓動,在配置功能,您可以設置您的全局變量:

public void Configure(IApplicationBuilder app, IHostingEnvironment env) 
{ 
    // Add framework services. 
    services.AddMvc(); 
    services.AddSingleton<IConfigurationRoot>(Configuration); 
    learningDIGlobalVariables.SetGlobals(app.ApplicationServices.GetService<IConfigurationRoot>(), env); 
} 
+0

而且,控制器如何在控制器內使用它? –

+0

它是公共類,所以你可以使用你現在使用它的方式,除了你不必初始化它。 – anserk

0

創建一個簡單的POCO來表示您的JSON配置。

public class AppSetting 
{ 
    public AppData AppData {set;get;} 
} 
public class AppData 
{ 
    public string ApplicationDeveloper { set;get;} 
} 

現在在Startup.cs類ConfigureServices方法中。

services.Configure<AppSettings>(Configuration); 

現在在您的控制器中,您將使用構造函數注入。你不需要在這裏新建任何對象。該框架將爲您注入依賴關係。

public class HomeController : Controller 
{ 
    private IOptions<AppSetting> _settings; 

    public HomeController(IOptions<AppSetting> settings) 
    { 
     this._settings= settings; 
    } 
} 

控制器內,可以使用this._settings.Value.AppData.ApplicationDeveloper讓你在你的JSON設置的值。

+0

得到此消息:ArgumentException:輸入'Microsoft.Extensions.Options.IOptions'1 [learningDI.AppSetting]'沒有默認構造函數 –