微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

由于其保护级别,“WebHost”无法访问

如何解决由于其保护级别,“WebHost”无法访问

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

首先,我初始化了一个新的空 ASP.NET Core Web 应用程序:

dotnet new web

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

dotnet add package Ocelot --version 17.0.0

然后我从教程中获取代码

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

但随后它抱怨在 BuildWebHost 方法调用的 WebHost 类“由于其保护级别而无法访问”。根据 Microsoft (https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.webhost) 的说法,WebHost“提供了使用预先配置的认值创建 IWebHost 和 IWebHostBuilder 实例的便捷方法。”,如下所示:

public static class WebHost
...

为什么它会抱怨 WebHost 无法访问,而该类实际上是公共的?我在这里错过了什么?

解决方法

documentation 开始,WebHost 位于命名空间 Microsoft.AspNetCore 中。但是在你的代码中,它没有使用这个命名空间。

在 Visual Studio 中,您可以尝试在 WebHost转到定义以发现类型的来源。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。