In ASP.NET 5 core there is an interface, IHostingEnvironment, but looking at the samples and source code for ASP.NET there are 3 different ways to obtain an instance.
Using an attribute...
[FromServices]
public IHostingEnvironment HostingEnvironment { get; set; }
As an argument passed via the configure method in the Startup class...
public void Configure(IApplicationBuilder application, IHostingEnvironment environment)
{
}
Or by using DI with either of the following lines...
var hostingEnvironment = requestServices.GetService<IHostingEnvironment>();
var hostingEnvironment = app.ApplicationServices.GetRequiredService<IHostingEnvironment>();
What is not clear is which is the correct method to use.
As an example of how unclear it can be, if a call is made to a method which requires an instance of a class which implements the IHostingEnvironment from within the Startup class, should the instance be passed as an argument in the method call, should DI be used or something else?