1
votes

I have a problem to implement Ocelot with .NET Core 3.0, when I try to add ocelot in my program class as the documentation specifies that should be done vs2019 shows me this error:

"IServiceCollection" doesn't contain a definition for "AddOcelot" or an accessible extension method "AddOcelot" that accepts a first argument of type "IServiceCollection" (is any directive using or an assembly reference missing?),

This error is also repeated for UseOcelot() method

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

what can I do to solve this Error?, I already installed the Nuget packages Ocelot version 13.8.0 and Ocelot.Provider.Consul version 13.8.0.

1
Make sure the necessary namespace is included ocelot.readthedocs.io/en/latest/introduction/… using Ocelot.DependencyInjection; using Ocelot.Middleware; - Nkosi
Was the provided answer below accurate? - Nkosi

1 Answers

4
votes

Make sure the necessary namespaces are included

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

Reference Ocelot : Getting Started

Source code for 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);
        }
    }
}

Source for middleware

namespace Ocelot.Middleware
{

    //...

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

    //...