I can't reference the IWebHostEnvironment element in my .NET Core class library. I have added NuGet packages Microsoft.AspNetCore.Hosting.Abstractions and Microsoft.Extensions.DepedencyInjection.Abstractions, but it still can't find the type. In the documentation, IWebHostEnvironment is in the Microsoft.AspNetCore.Hosting.Abstractions assembly, but I can't seem to reference the correct assembly.
Is there any other assembly I need to reference?
Project file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>LundbeckConsulting.Components.Core</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.6" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.6" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LC.Components\LC.Components.csproj" />
</ItemGroup>
</Project>
This type is obsolete and will be removed in a future version. The recommended alternative is Microsoft.AspNetCore.Hosting.IWebHostEnvironment. Learn how the Startup class in ASP.NET Core configures services and the app's request pipeline. Discover how to handle errors in ASP.NET Core apps.
It is recommended that you use IWebHostEnvironment instead. The reasoning behind this presumably was that IHostingEnvironment has multiple implementations for the same type in .NET Core in different packages. The AspNetCore specific version in Microsoft.AspNetCore.Hosting looks like this:
In the code blow, a red kind of line appears below the IWebHostEnvironment type for the env parameter saying that "The type or namespace 'IWebHostEnvironment' could not be found (are you missing a using directive or an assembly reference?)" public async Task Invoke (HttpContext context, IWebHostEnvironment env) { // Some code here.
Use hosting startup assemblies in ASP.NET Core. An IHostingStartup (hosting startup) implementation adds enhancements to an app at startup from an external assembly. For example, an external library can use a hosting startup implementation to provide additional configuration providers or services to an app.
Please add this to your project file:
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
You shouldn't need to specify any other packages for framework. Please follow this link
Pay attention to FrameworkReference attribute.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With