1

我在使用 .NET Core 3.0 实现 Ocelot 时遇到问题,当我尝试在我的程序类中添加 ocelot 时,因为文档指定应该完成 vs2019 向我显示此错误:

“IServiceCollection”不包含“AddOcelot”的定义或接受“IServiceCollection”类型的第一个参数的可访问扩展方法“AddOcelot”(是否有任何指令使用或缺少程序集引用?),

UseOcelot()方法也重复此错误

public class Program
{
    public static void Main(string[] args)
    {
        new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .ConfigureAppConfiguration((context, config) => 
            {
                config
                    .SetBasePath(context.HostingEnvironment.ContentRootPath)
                    .AddJsonFile("appsettings.json", true, true)
                    .AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", true, true)
                    .AddJsonFile("ocelot.json")
                    .AddEnvironmentVariables();
            }).ConfigureServices(s => {
                s.AddOcelot().AddConsul();
            }).ConfigureLogging((hostingContext, logging) =>
            {
                logging.AddConsole();
            })
            .UseIIS()
            .Configure(app =>
            {
                app.UseOcelot().Wait();
            })
            .Build().Run();
    }
}

我可以做些什么来解决这个错误?,我已经安装了 Nuget 包 Ocelot 版本 13.8.0 和 Ocelot.Provider.Consul 版本 13.8.0。

4

1 回答 1

5

确保包含必要的命名空间

using Ocelot.DependencyInjection; //For Dependency Injection
using Ocelot.Middleware; //For middleware

参考Ocelot:入门

DI的源代码

namespace Ocelot.DependencyInjection
{
    public static class ServiceCollectionExtensions
    {
        public static IOcelotBuilder AddOcelot(this IServiceCollection services)
        {
            var configuration = services.BuildServiceProvider()
                .GetRequiredService<IConfiguration>();
            return new OcelotBuilder(services, configuration);
        }

        public static IOcelotBuilder AddOcelot(this IServiceCollection services, IConfiguration configuration)
        {
            return new OcelotBuilder(services, configuration);
        }
    }
}

中间件的来源

namespace Ocelot.Middleware
{

    //...

    public static class OcelotMiddlewareExtensions
    {
        public static async Task<IApplicationBuilder> UseOcelot(this IApplicationBuilder builder)
        {
            await builder.UseOcelot(new OcelotPipelineConfiguration());
            return builder;
        }

    //...
于 2020-01-09T15:56:48.600 回答