gpt4 book ai didi

c# - 将 Ocelot 16.0 与 ASP.Net Core 3.1 集成无法正常工作,因为我需要将 Swagger 与 Ocelot 一起使用

转载 作者:行者123 更新时间:2023-12-02 16:36:43 27 4
gpt4 key购买 nike

我已经将 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 错误。请协助我解决这个问题。

提前致谢。

最佳答案

尝试在您的 JSON 文件中将 ReRoutes 重命名为 Routes

关于c# - 将 Ocelot 16.0 与 ASP.Net Core 3.1 集成无法正常工作,因为我需要将 Swagger 与 Ocelot 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62573042/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com