嗯,我的第一种方法是使用两个独立的应用程序,它们按照自己的条件运行.您已经提到,其他服务应该访问主应用程序的状态这也是我会避免的(具有进程中状态,这也不利于扩展,具有一些其他缺点),但如果您更喜欢保持当前配置,您可以拥有如下所示:
// start up configuration
app.MapControllers();
app.Use(async (ctx, next) =>
{
var port = ctx.Request.HttpContext.Connection.LocalPort;
if (port == 7050)
{
// Do not allow FooController
if (ctx.Request.RouteValues["controller"]?.ToString() == "Foo")
{
ctx.Response.StatusCode = 404;
return; // to prevent the other middlewares to run
}
}
else
{
// Allow only FooController
if (ctx.Request.RouteValues["controller"]?.ToString() != "Foo")
{
ctx.Response.StatusCode = 404;
return; // to prevent the other middlewares to run
}
}
await next(ctx);
});
app.Run();
您的设置中的某个位置:
"applicationUrl": "https://localhost:7049;https://localhost:7050;", #launchSettings.json
or
"urls":"https://localhost:7049;https://localhost:7050;" #appSettings.json