我用 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'
但它也不起作用 .