I build an ASP.NET Core application and I create a .NET Core Class Library for unit testing.
I want to use IHostingEnvironment
in my library (to get physical path of a file), so I've added this line to Startup.cs of my ASP.NET Core application :
services.AddSingleton<IHostingEnvironment>();
In the Library I've added reference to my ASP.NET application, and in my class I wrote this:
private IHostingEnvironment _env;
public Class1(IHostingEnvironment env)
{
_env = env;
}
But when I run it then it gives me this error:
the following constructor parameters did not have matching fixture date : IHostingEnvironment env
What is the problem? How can I use it in .NET Core Class Library?
EDIT: I tried to use this too:
IServiceCollection services = new ServiceCollection();
services.AddSingleton<IHostingEnvironment>();
IServiceProvider provider = services.BuildServiceProvider();
IHostingEnvironment service = provider.GetService<IHostingEnvironment>();
var p = service.WebRootPath;
The last one gives me this error:
Cannot instantiate implementation type 'Microsoft.AspNetCore.Hosting.IHostingEnvironment' for service type 'Microsoft.AspNetCore.Hosting.IHostingEnvironment'