天天看點

aspnetcore java,在.NetCore庫中使用IHostingEnvironment

我用 Asp.net core 建構了一個應用程式,我為單元測試建立了一個 .Net core class library ,我想在我的庫中使用 IHostingEnvironment (用于擷取檔案的實體路徑),是以我将它添加到我的Asp.net核心應用程式服務的startup.cs中:

services.AddSingleton();

在庫中我添加了對我的Asp.net應用程式的引用,并在我的庫中的類我寫道:

private IHostingEnvironment _env;

public Class1(IHostingEnvironment env)

{

_env = env;

}

但是當我運作它時,它給了我這個錯誤:

以下構造函數參數沒有比對的fixture日期:IHostingEnvironment env

問題是什麼?我如何在 .NetCore library 中使用它?

編輯:我也這樣使用:

IServiceCollection services = new ServiceCollection();

services.AddSingleton();

IServiceProvider provider = services.BuildServiceProvider();

IHostingEnvironment service = provider.GetService();

var p = service.WebRootPath; // give this error: Cannot instantiate implementation type 'Microsoft.AspNetCore.Hosting.IHostingEnvironment' for service type 'Microsoft.AspNetCore.Hosting.IHostingEnvironment'

但它也不起作用 .