Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IHostBuilder does not contain a definition for ConfigureWebHostDefaults

I'm trying to use the new GenericHost in .NET Core 3.0 documented here but I'm getting a really basic error that stats IHostBuilder does not contain a definition for the ConfigureWebHostDefaults function.

Looking at the ASP.NET 3.0 documentation here for the IHostBuilder interface here I cant see any reference to ConfigureWebHostDefaults so I'm a bit confused.

I'm using the 3.0.0 packages for Microsoft.Extensions.Hosting and Microsoft.Extensions.Hosting.Abstractions but cant help but feel I'm either missing something really obvious or that ConfigureWebHostDefaults has for some reason been removed?

Update -- Screen shot of Program.cs

enter image description here

Update -- .csproj file

<Project Sdk="Microsoft.NET.Sdk">    <PropertyGroup>     <OutputType>Exe</OutputType>     <TargetFramework>netcoreapp3.0</TargetFramework>   </PropertyGroup>    <ItemGroup>     <PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.7" />     <PackageReference Include="Microsoft.Extensions.Hosting" Version="3.0.0" />     <PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.0.0" />   </ItemGroup>  </Project> 
like image 447
Tim Avatar asked Sep 25 '19 11:09

Tim


People also ask

What does ConfigureWebHostDefaults do?

ConfigureWebHostDefaults(IHostBuilder, Action<IWebHostBuilder>) Configures a IHostBuilder with defaults for hosting a web app. This should be called before application specific configuration to avoid it overwriting provided services, configuration sources, environments, content root, etc.

What is IHostBuilder in .NET core?

What is Host Builder? Host Builder is a static class that provides two methods and when we call these methods, they will add some features into ASP.NET Core Applications. The Host Builder Provides two convenient methods for creating instances of IHostBuilder with pre-configured defaults.

What is IWebHostEnvironment?

IWebHostEnvironment Provides information about the web hosting environment an application is running in. belongs to namespace Microsoft.AspNetCore.Hosting. The IWebHostEnvironment interface need to be injected as dependency in the Controller and then later used throughout the Controller.

What is generic host?

The Generic Host can be used with other types of . NET applications, such as Console apps. A host is an object that encapsulates an app's resources and lifetime functionality, such as: Dependency injection (DI) Logging.


Video Answer


1 Answers

Along with using Microsoft.Extensions.Hosting, ConfigureWebHostDefaults also require using Microsoft.AspNetCore.Hosting; as follows:

using Microsoft.AspNetCore.Hosting; //<-- Here it is using Microsoft.Extensions.Hosting;  namespace MyApp {     public class Program     {         public static void Main(string[] args)         {             CreateWebHostBuilder(args).Build().Run();         }          public static IHostBuilder CreateWebHostBuilder(string[] args) =>             Host.CreateDefaultBuilder(args)                 .ConfigureWebHostDefaults(webBuilder =>                 {                     webBuilder.UseStartup<Startup>();                 });     } } 

Moreover it look like your project is a Console Application. That is the problem. ConfigureWebHostDefaults is for Web Application only. So you can convert your Console Application into Web Applicaton by replacing Sdk="Microsoft.NET.Sdk" with Sdk="Microsoft.NET.Sdk.Web" in your .csproj file as follows:

<Project Sdk="Microsoft.NET.Sdk.Web">    <PropertyGroup>     <TargetFramework>netcoreapp3.0</TargetFramework>     <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>   </PropertyGroup>  </Project> 
like image 167
TanvirArjel Avatar answered Sep 22 '22 08:09

TanvirArjel