C# Ocelot没有';重新定义微服务
我正在尝试实现我的微服务应用程序。 我已经在localhost:5001-base CRUD上编目了API微服务。 我想用Ocelot实现Api网关 Catalo.API launsetings.json:C# Ocelot没有';重新定义微服务,c#,asp.net-core,microservices,ocelot,C#,Asp.net Core,Microservices,Ocelot,我正在尝试实现我的微服务应用程序。 我已经在localhost:5001-base CRUD上编目了API微服务。 我想用Ocelot实现Api网关 Catalo.API launsetings.json: "reservation_system.Catalo.Api": { "commandName": "Project", "launchBrowser": true, "launchUrl": "swagger/index.html", "applicationUrl": "
"reservation_system.Catalo.Api": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger/index.html",
"applicationUrl": "http://localhost:5001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
API网关中的Program.cs:
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>();
}
我正试图通过
我得到了“你好,世界!”的回答。
这里有什么问题?没有执行Ocelot中间件,因为您通过调用
Run()
委托并写入响应流使请求管道短路
中间件组件在Configure
方法中注册的顺序很重要。这些组件的调用顺序与它们的添加顺序相同
所以如果你移动等待app.UseOcelot()
up,进入Configure()
方法,就在app.Run()
之前,将执行Ocelot中间件
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();
}
}