将 Ocelot 16.0 与 ASP.Net Core 3.1 集成不起作用,因为我需要将 Swagger 与 Ocelot 一起使用
Posted
技术标签:
【中文标题】将 Ocelot 16.0 与 ASP.Net Core 3.1 集成不起作用,因为我需要将 Swagger 与 Ocelot 一起使用【英文标题】:Integrating Ocelot 16.0 with ASP.Net Core 3.1 not working as I need to use Swagger with Ocelot 【发布时间】:2020-10-15 18:35:22 【问题描述】:我已经将 Ocelot 与 Asp.Net Core 2.1 一起使用并且可以正常工作,但是当与 Asp.Net Core 3.1 一起使用时,它根本无法正常工作。在我看来,它没有选择“ocelot.json”文件。以下是我的 Program.cs 文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace ApiGateway
public class Program
public static void Main(string[] args)
CreateHostBuilder(args).Build().Run();
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
config
.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.hostingContext.HostingEnvironment.EnvironmentName.json", optional: true, reloadOnChange: true)
.AddJsonFile($"ocelot.hostingContext.HostingEnvironment.EnvironmentName.json")
.AddEnvironmentVariables();
)
.ConfigureWebHostDefaults(webBuilder =>
webBuilder.UseStartup<Startup>();
);
下面是 Startup.cs 文件:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
namespace ApiGateway
public class Startup
private IConfiguration configuration;
public Startup(IConfiguration configuration)
this.configuration = configuration;
// This method gets called by the runtime. Use this method to add services to the container.
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 void Configure(IApplicationBuilder app, IWebHostEnvironment env)
if (env.IsDevelopment())
app.UseDeveloperExceptionPage();
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
endpoints.MapGet("/", async context =>
await context.Response.WriteAsync("API GATEWAY FUNCIONANDO");
);
);
app.UseOcelot().Wait();
以下是 ocelot.Development.json 文件:
"ReRoutes": [
"DownstreamPathTemplate": "/TestApi/api/test",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
"Host": "localhost",
"Port": "80"
],
"UpstreamPathTemplate": "/getTest"
,
"DownstreamPathTemplate": "/TestMicroS/api/values",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
"Host": "localhost",
"Port": "82"
],
"UpstreamPathTemplate": "/getValues"
],
"GlobalConfiguration":
我已经参考了 YouTube 上的所有上述配置,但它不起作用。本地网站正在运行,但是当我尝试点击 UpStream 路径时,它给了我 localhost not found 404 错误。请帮我解决这个问题。
提前致谢。
【问题讨论】:
【参考方案1】:尝试在 JSON 文件中将 ReRoutes
重命名为 Routes
。
【讨论】:
是的。你说得对。 Ocelot 16 有不同格式的配置文件。看看ref。以上是关于将 Ocelot 16.0 与 ASP.Net Core 3.1 集成不起作用,因为我需要将 Swagger 与 Ocelot 一起使用的主要内容,如果未能解决你的问题,请参考以下文章
如何在 ocelot.json 路由 asp.net core 中发布 json body
ASP.NET Core 3.1 中的 Ocelot API Gateway 自定义聚合器问题
Api网关Kong集成Consul做服务发现及在Asp.Net Core中的使用