Ocelot не переходит на микросервис

Я пытаюсь реализовать свое приложение для микросервисов. У меня есть микросервис Catalog API на localhost: 5001 - базовый CRUD. Я хочу реализовать Api Gateway с помощью 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 из 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();
        }
    }

Я пытаюсь получить доступ к своему API каталога через http://localhost:50121/catalog. Я получаю " Привет, мир!" отвечать. Что здесь за проблема?


person Kenik    schedule 14.08.2019    source источник


Ответы (1)


Промежуточное ПО Ocelot не выполняется, потому что вы замыкаете конвейер запросов, вызывая делегат Run() и записывая в поток ответа.

Порядок регистрации компонентов промежуточного программного обеспечения в Configure методе имеет значение. Эти компоненты вызываются в том же порядке, в котором они добавляются.

Таким образом, если вы переместите await app.UseOcelot(); вверх, в метод Configure(), прямо перед app.Run(), будет выполнено промежуточное ПО Ocelot.

person user1336    schedule 14.08.2019