Search code examples
asp.net-core-3.1.net-core-3.1ocelot

.net core Ocelot A public method named 'ConfigureDevelopment' or 'Configure' could not be found


The code below belongs to .net core web api, which I call OcelotApiGateway.

I installed Ocelot 16.0.1 via nuget package manager on the OcelotApiGateway.

this is my Program.cs

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;

namespace OcelotApiGateway
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.ConfigureAppConfiguration((host, config) => config.AddJsonFile("ocelot.json"));
                    webBuilder.UseStartup<Startup>();
                });
    }
}

and this is Startup.cs

using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;

namespace OcelotApiGateway
{
    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.AddControllers();
            services.AddOcelot(Configuration);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public async Task ConfigureAsync(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

            await app.UseOcelot();
        }
    }
}

But at the line of 'CreateHostBuilder(args).Build().Run();' in Program.cs, this error occurring

System.InvalidOperationException: 'A public method named 'ConfigureDevelopment' or 'Configure' could not be found in the 'OcelotApiGateway.Startup' type.'


Solution

  • I solved the problem, it was my fault. When I added await in Configure() methot, this method has been changed by Visual studio automatically. As below. I realized this later.

    public async Task ConfigureAsync(IApplicationBuilder app, IWebHostEnvironment env)
    

    This method must be:

    public async void Configure(IApplicationBuilder app, IWebHostEnvironment env)