pmn
pmn

Reputation: 2247

Using IHostingEnvironment in .NetCore library

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'

Upvotes: 12

Views: 37157

Answers (5)

Reyan Chougle
Reyan Chougle

Reputation: 5311

This worked for me in both .net core class library and console application:

Using references,

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Internal;

Adding DI registration,

services.AddSingleton<IHostingEnvironment, HostingEnvironment>();

Upvotes: 4

mohaa8844
mohaa8844

Reputation: 441

Try this, its simple enough

private IHostEnvironment env;
public Startup(IHostEnvironment env)
{
    this.env = env;
}
public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<IHostEnvironment>(env);
}

then you can use it in your class

private IHostingEnvironment _env;
public Class1(IHostingEnvironment env)
{
    _env = env;
}

hope it does the job ^_^

Upvotes: 0

Max
Max

Reputation: 3328

A note for reference as I ended up here.

If you target netstandard (netstandard2.0) in your class library, add Microsoft.Extensions.Hosting.Abstractions from NuGet to get the IHostingEnvironment interface without any implementations.

I know question specifies .net core, anyways.. might help out those being where I were.

Upvotes: 2

Arhire Ionut
Arhire Ionut

Reputation: 460

For my .net class library all I had to do is install the following nuget package for version 2.1.0:

Microsoft.AspNetCore.Hosting.Abstractions

https://www.nuget.org/packages/Microsoft.AspNetCore.Hosting.Abstractions/

and then I just injected IHostingEnvironment into my constructor.

I didn't even need to modify Startup.cs

Upvotes: 5

Joel Harkes
Joel Harkes

Reputation: 11661

Note: services.AddSingleton<IHostingEnvironment>(); means you are registering IHostingEnvironment as an implementation for IHostingEnvironment in a singleton scope (always reuse).

Since you can't create an instance of an interface, you get this error.

solution

define the class you want to be created (that implements IHostingEnvironment), eg:

services.AddSingleton<IHostingEnvironment>(new HostingEnvironment());

Behind the scenes dotnet core (Hosting nuget package)

In the WebHostBuilder The first row in the constructor is:

this._hostingEnvironment = (IHostingEnvironment) new HostingEnvironment();

This hosting environment is later filled with more settings, by the webhost builder.

You should look at their github page or decompile the sources: https://github.com/aspnet/Hosting

Note: Most of the properties/settings of HostingEnvironment are set on Build() method of the WebHostBuilder. If you want to moq/test this yourself you should set these properties yourself or just also include the WebHostBuilder in your test.

Upvotes: 12

Related Questions