Resolve type or namespace name IWebHostEnvironment could not be found

In one of earlier posts Abstracting .Net Core Startup Code, I wrote about how to abstract .Net Core Web Application's start up code into a separate library. I got some questions from some fellow developers that they have run into errors like below when abstracting the code.

Error	CS0246	The type or namespace name 'IWebHostEnvironment' could not be found 
  (are you missing a using directive or an assembly reference?)		

If you try to find definition of this interface, it will point to Microsoft.AspNetCore.Hosting.Abstractions assembly. If you will search in NuGet packages, you will find a package with that name as well. Adding that package is not going to help. To resolve this error, you will need to make a change in your project file. If you open csproj file of your class library project, you will notice the root node has following value.

<Project Sdk="Microsoft.NET.Sdk">

If you open Frameworks node under Dependencies of your class library, you will find that only one item, Microsoft.NetCore.App. Change the cpproj file's root node's Sdk value to Microsoft.NET.Sdk.Web. You will notice that an additional item, Microsoft.AspNetCore.App, shows up in Dependencies/Frameworks of your class library project. Now you will be able to add following lines in your source file to resolve the error.

using Microsoft.AspNetCore.Hosting;

This should take care of the error and you will be on your way to abstracting any implementation related to IWebHostEnvironment interface.




17.3 °C / 63.1 °F

weather conditions Clear

Monthly Posts

Blog Tags