繁体   English   中英

错误 CS0246:找不到类型或命名空间名称“IWebHostEnvironment”(您是否缺少 using 指令或程序集引用?)

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

我正在尝试为 .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?}");
            });
        }
    }
}

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM