14

In asp.net core 2.1 I could create IHostingEnvironment like this:

public IHostingEnvironment CreateHostingEnvironment()
{
    var hosting = new HostingEnvironment()
    {
       EnvironmentName = "IntegrationTests"
    };
    return hosting;
}

In Asp.net core 3.1 it was changed to IWebHostEnvironment but I need to create it similar way. May goal is to create this object and set Environment name.

public IWebHostEnvironment CreateWebHostEnvironment()
{
    var host = new WebHostEnvironment(); // ???
}
4
  • It's internal in ASP.NET Core 3.x. Depending on your use case, you should probably consider creating your own class that implements the same interfaces.
    – mm8
    Mar 27, 2020 at 11:03
  • @mm8, this is the old 2.1 class. The new is this one Mar 27, 2020 at 11:37
  • You have linked to the interface. I've linked to the implementation which is internal. The internal class implements IWebHostEnvironment.
    – mm8
    Mar 27, 2020 at 12:20
  • @DeivydasVoroneckis Please describe what do you want to achieve, maybe you don't need to create instance of this class. Mar 30, 2020 at 0:43

1 Answer 1

16

The only built-in implementation of the IWebHostEnvironment interface is internal in ASP.NET Core 3.x:

internal class HostingEnvironment : IHostingEnvironment, Extensions.Hosting.IHostingEnvironment, IWebHostEnvironment
{
    public string EnvironmentName { get; set; } = Extensions.Hosting.Environments.Production;

    public string ApplicationName { get; set; }

    public string WebRootPath { get; set; }

    public IFileProvider WebRootFileProvider { get; set; }

    public string ContentRootPath { get; set; }

    public IFileProvider ContentRootFileProvider { get; set; }
}

So if you need to create an instance of a class that implements the interface for some reason, you could basically just copy the above code into your project and perhaps change the name of the class. You can then create instances of it as per your requirements.

The framework depends on the interface only anyway.

4
  • But isn't it possible to create a real Hosting environment class by using some similar build methods like in program.cs file? Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); This is for integration tests, so I'm ok with real implementation Mar 28, 2020 at 17:23
  • No, not without using reflection anyway. But creating an instance of HostingEnvironment won't help. ASP.NET Core supports integration tests using a test web host and an in-memory test server as explained in the docs.
    – mm8
    Mar 31, 2020 at 6:54
  • Ok, thank you. Then I will simply mock the interface. Apr 1, 2020 at 7:01
  • Thanks, this solved the particular issue I was having. I just needed to instantiate a 'dummy' HostEnvironment so that I could use the razor services in a console app.
    – Andrew
    Dec 3, 2020 at 23:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.