。net core 2.1 ocelot网关返回404

问题描述 投票:0回答:1

我正在尝试在我的应用程序中实现ocelot网关,但它总是在我在ocelot.json中配置的所有路径上返回404。

[无论何时我使用简单调用或聚合调用在Postman上进行GET操作,它总是返回404并且程序保持运行,不会崩溃。

Ocelot.json:

{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/api/Buildings",
      "UpstreamPathTemplate": "/allBuildings",
      "UpstreamHttpMethod": [ "Get" ],
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5001
        }
      ],
      "Key": "Buildings"
    },
    {
      "DownstreamPathTemplate": "/api/Device",
      "UpstreamPathTemplate": "/allDevices",
      "UpstreamHttpMethod": [ "Get" ],
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5001
        }
      ],
      "Key": "Devices"
    }
  ],
  "Aggregates": [
    {
      "ReRouteKeys": [
        "Buildings",
        "Devices"
      ],
      "UpstreamPathTemplate": "/BAD",
      "Aggregator":  "FakeDefinedAggregator"
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "https://localhost:44351/"
  }
}

Program.cs:

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

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }

Startup.cs:

public class Startup
    {
        public IConfiguration Configuration { get; }

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

        public void ConfigureServices(IServiceCollection services)
        {

            services.AddOcelot(Configuration);
        }

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

         //   await app.UseOcelot();

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

我正在使用.NET Core 2.1和Ocelot NuGet软件包版本13.5.1。

c# asp.net-core gateway ocelot
1个回答
0
投票

如果使用请求聚合,则需要向Ocelot注册IDefinedAggregator实现。

赞:

public void ConfigureServices(IServiceCollection services)
{
    services.AddOcelot(Configuration).AddSingletonDefinedAggregator<FakeDefinedAggregator>();
}

否则,Ocelot将无法在容器中找到您的聚合器。

© www.soinside.com 2019 - 2024. All rights reserved.