【问题标题】:error CS0246: The type or namespace name 'IWebHostEnvironment' could not be found (are you missing a using directive or an assembly reference?)错误 CS0246:找不到类型或命名空间名称“IWebHostEnvironment”(您是否缺少 using 指令或程序集引用?)
【发布时间】:2020-01-12 13:32:15
【问题描述】:

我正在尝试为 .net 应用程序运行 docker build,源代码在这里:https://github.com/NileshGule/AKS-learning-series/tree/master/src

按照以下步骤我得到错误:

Web/Startup.cs(30,56): error CS0246: The type or namespace name 'IWebHostEnvironment' could not be found (are you missing a using directive or an assembly reference?) [/TechTalksWeb/TechTalksWeb.csproj]

我相信这在 .Net 3.0 中已被弃用,但我在 .Net 2.1 上

dotnet --version                                                       
2.1.607

下面构建的完整输出

➜  TechTalksWeb git:(master) ✗ docker build .                                                                                                     
Sending build context to Docker daemon  6.879MB
Step 1/11 : FROM microsoft/dotnet:2.1.300-sdk  AS build-env
 ---> 90a5a2ee9755
Step 2/11 : WORKDIR /TechTalksWeb
 ---> Running in bf2efd408659
Removing intermediate container bf2efd408659
 ---> b5c1c0a8f026
Step 3/11 : COPY NuGet.config ./
 ---> 2fbe7f9eb0ba
Step 4/11 : COPY TechTalksWeb.csproj ./
 ---> 49e1c29e4144
Step 5/11 : RUN dotnet restore
 ---> Running in bfb23ac61bbf
  Restoring packages for /TechTalksWeb/TechTalksWeb.csproj...
  Generating MSBuild file /TechTalksWeb/obj/TechTalksWeb.csproj.nuget.g.props.
  Generating MSBuild file /TechTalksWeb/obj/TechTalksWeb.csproj.nuget.g.targets.
  Restore completed in 1.4 sec for /TechTalksWeb/TechTalksWeb.csproj.
Removing intermediate container bfb23ac61bbf
 ---> 2b5068160dd8
Step 6/11 : COPY . ./
 ---> d6e2551f5bfb
Step 7/11 : RUN dotnet publish --configuration Release --output releaseOutput --no-restore
 ---> Running in f08fbc49c68f
Microsoft (R) Build Engine version 15.7.179.6572 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

Web/Startup.cs(30,56): error CS0246: The type or namespace name 'IWebHostEnvironment' could not be found (are you missing a using directive or an assembly reference?) [/TechTalksWeb/TechTalksWeb.csproj]
The command '/bin/sh -c dotnet publish --configuration Release --output releaseOutput --no-restore' returned a non-zero code: 1

有问题的行是 startup.cs 的一部分

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

完整代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace Web
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}

【问题讨论】:

    标签: c# .net docker asp.net-core .net-core


    【解决方案1】:

    据我所知,它被称为 IWebHostEnvironment,用于 .NET Core 3.0。

    在 .NET Core 2.1 中,您需要使用 IHostingEnvironment(在 3.0 中已弃用)。

    【讨论】:

      猜你喜欢
      • 2020-01-31
      • 2019-12-02
      • 1970-01-01
      • 2021-08-20
      • 2020-02-19
      • 1970-01-01
      • 1970-01-01
      • 2022-06-15
      • 2020-08-29
      相关资源
      最近更新 更多