IHostBuilder不包含ConfigureWebHostDefaults的定义 [英] IHostBuilder does not contain a definition for ConfigureWebHostDefaults

查看:58
本文介绍了IHostBuilder不包含ConfigureWebHostDefaults的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 .NET Core 3.0 中记录的

更新-.csproj文件

 < 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> 

解决方案

一起使用Microsoft.Extensions.Hosting ConfigureWebHostDefaults 也要求使用Microsoft.AspNetCore .hosting; 如下:

 使用Microsoft.AspNetCore.Hosting;//<-在这里使用Microsoft.Extensions.Hosting;命名空间MyApp{公共课程{公共静态无效的Main(string [] args){CreateWebHostBuilder(args).Build().Run();}公共静态IHostBuilder CreateWebHostBuilder(string [] args)=>Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder =>{webBuilder.UseStartup< Startup>();});}} 

此外,您的项目看起来像是控制台应用程序.那就是问题所在. ConfigureWebHostDefaults 仅适用于Web应用程序.因此,您可以将 Sdk ="Microsoft.NET.Sdk" 替换为 Sdk ="Microsoft.NET.Sdk.Web" 在您的 .csproj 文件如下:

 < Project Sdk ="Microsoft.NET.Sdk.Web">< PropertyGroup>< TargetFramework> netcoreapp3.0</TargetFramework>< AspNetCoreHostingModel> InProcess</AspNetCoreHostingModel></PropertyGroup></Project> 

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

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>

解决方案

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>

这篇关于IHostBuilder不包含ConfigureWebHostDefaults的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆