简体   繁体   English

'WebHost' 由于其保护级别而无法访问

[英]'WebHost' is inaccessible due to its protection level

I am trying to follow Microsoft's Ocelot API Gateway tutorial ( https://docs.microsoft.com/en-us/dotnet/architecture/microservices/multi-container-microservice-net-applications/implement-api-gateways-with-ocelot ).我正在尝试遵循 Microsoft 的 Ocelot API 网关教程( https://docs.microsoft.com/en-us/dotnet/architecture/microservices/multi-container-microservice-net-applications/implement-api-gateways-with-ocelot )。

First I intialized a new empty ASP.NET Core web app:首先我初始化了一个新的空 ASP.NET Core web 应用程序:

dotnet new web

Then I installed the Ocelot dependencies ( https://www.nuget.org/packages/Ocelot/ ):然后我安装了 Ocelot 依赖项( https://www.nuget.org/packages/Ocelot/ ):

dotnet add package Ocelot --version 17.0.0

Then I took the code from the tutorial:然后我从教程中获取了代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
using System.IO;

namespace MyApp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args)
        {
            var builder = WebHost.CreateDefaultBuilder(args);

            builder.ConfigureServices(s => s.AddSingleton(builder))
                    .ConfigureAppConfiguration(
                          ic => ic.AddJsonFile(Path.Combine("configuration",
                                                            "configuration.json")))
                    .UseStartup<Startup>();
            var host = builder.Build();
            return host;
        }
    }
}

But then it complains that the WebHost class, called in BuildWebHost method, "is inaccessible due to its protection level".但随后它抱怨在 BuildWebHost 方法中调用的 WebHost class“由于其保护级别而无法访问”。 According to Microsoft ( https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.webhost ), WebHost "provides convenience methods for creating instances of IWebHost and IWebHostBuilder with pre-configured defaults.", and looks like so:根据 Microsoft ( https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.webhost ) 的说法,WebHost “为创建具有预配置默认值的 IWebHost 和 IWebHostBuilder 实例提供了便利的方法。”,以及看起来像这样:

public static class WebHost
...

Why does it complain that WebHost is inaccessible, when the class is in fact public?当 class 实际上是公开的时,为什么它抱怨 WebHost 无法访问? What am I missing here?我在这里想念什么?

From the documentation , WebHost is in the namespace Microsoft.AspNetCore .文档来看, WebHost位于命名空间Microsoft.AspNetCore中。 But in your code, It hasn't the using to this namespace.但是在您的代码中,它没有使用此命名空间。

In Visual Studio, you can try Go to definition on WebHost to discover where the type come.在 Visual Studio 中,您可以尝试在WebHost上定义 Go以发现类型来自何处。

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

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