0

I'm trying to implement my microservice application. I've Catalog API microservice on localhost:5001 - base CRUD. I want to implement Api Gateway using Ocelot.

Catalo.API launSettings.json:

"reservation_system.Catalo.Api": {
  "commandName": "Project",
  "launchBrowser": true,
  "launchUrl": "swagger/index.html",
  "applicationUrl": "http://localhost:5001",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
}

Program.cs from API Gateway:

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

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((host, config) =>
                {
                    config
                    .AddJsonFile("appsettings.json", true, true)
                    .AddJsonFile($"appsettings.{host.HostingEnvironment.EnvironmentName}.json", true, true)
                    .AddEnvironmentVariables();
                    config.AddJsonFile("configuration.json");
                })
            .UseStartup<Startup>();
    }

Startup.cs

public class Startup
    {
        public IConfiguration Configuration { get; set; }

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            services.AddOcelot();
        }

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

            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
            app.UseMvc();
            await app.UseOcelot();
        }
    }

I'm trying to reach my catalog API through http://localhost:50121/catalog I'm getting "Hello World!" answer. What's an issue here?

user1336
  • 6,435
  • 2
  • 27
  • 34
Kenik
  • 103
  • 1
  • 15

1 Answers1

1

The Ocelot middleware is not executed because you are short-circuiting the request pipeline by calling the Run() delegate and writing to the response stream.

The order that middleware components are registered in the Configure method matters. These components are invoked in the same order they are added.

So if you move await app.UseOcelot(); up, into the Configure() method, right before the app.Run(), the Ocelot middleware will be executed.

user1336
  • 6,435
  • 2
  • 27
  • 34