Asp.net core 在运行时更改Kestrel端口
我的项目使用.net core 3.1,是一个使用内置服务器Kestrel的web应用程序。Asp.net core 在运行时更改Kestrel端口,asp.net-core,port,kestrel,Asp.net Core,Port,Kestrel,我的项目使用.net core 3.1,是一个使用内置服务器Kestrel的web应用程序。 在CreateWebHostBuilder()期间,我使用.UseUrls(“”)设置端口,并按其应该的方式工作 在我的界面中,我希望用户能够在运行时更改端口。 但我必须使用新保存的配置重新启动应用程序才能使其正常工作。 是否有关于如何在运行时更改它的提示?public void Configure(IAApplicationBuilder应用程序) { var address=app.ServerFe
在CreateWebHostBuilder()期间,我使用.UseUrls(“”)设置端口,并按其应该的方式工作 在我的界面中,我希望用户能够在运行时更改端口。
但我必须使用新保存的配置重新启动应用程序才能使其正常工作。
是否有关于如何在运行时更改它的提示?
public void Configure(IAApplicationBuilder应用程序)
{
var address=app.ServerFeatures.Get();
address.address.Clear();
address.address.Add(“http://*:5556”);
}
我解决了Kestrel的重启服务器功能(又快又脏)。这种方法只是一种技巧,不应该在生产中使用。也许有一些聪明的人可以把事情做得更好。
public class Program
{
//Simple bool to tell the host to load again
public static bool RestartWebserver { get; set; } = false;
//Port to use
public static int HttpPort { get; set; } = 80;
//Main program
public static int Main(string[] args)
{
//Infinityloop if
while(true)
{
CreateWebHostBuilder(args, Directory.GetCurrentDirectory()).Build().Run();
//If RestartWebserver is false, exit everything...
if (!RestartWebserver)
{
Console.WriteLine("Restarting...");
break;
}
//Reset for the "new" host to be created.
RestartWebserver = false;
}
}
//Helper for creating host, returns a IWebHostBuilder to be Build and runned.
public static IWebHostBuilder CreateWebHostBuilder(string[] args, string ContentRoot)
{
var Address = "0.0.0.0";
string http = "http://" + Address + ":" + HttpPort;
//Add arguments to the config (we could have a port set here too..)
var configuration = new ConfigurationBuilder()
.AddCommandLine(args)
.Build();
Startup.Configuration = configuration;
//Return Webhost
return WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseContentRoot(ContentRoot)
.UseKestrel()
.UseUrls(new string[] { http }));
}
}
//Now in any controller you can force close the host like this
public class RestartController : Controller
{
public IHostApplicationLifetime _applicationLifetime;
public StatusController(IHostApplicationLifetime applicationLifetime)
{
_applicationLifetime = applicationLifetime;
}
public async Task<IActionResult> Index()
{
//Set new port
Program.HttpPort = 12345;
//Set the server to restart
Program.RestartWebserver = true;
//Quit current host
_applicationLifetime.StopApplication();
//This function will fail.
//Suggest to create a javascript file to reload client to the new port...
return View("Index");
}
}
公共类程序
{
//告诉主机重新加载的简单bool
公共静态bool RestartWebserver{get;set;}=false;
//要使用的端口
公共静态int-HttpPort{get;set;}=80;
//主程序
公共静态int Main(字符串[]args)
{
//无穷远
while(true)
{
CreateWebHostBuilder(args,Directory.GetCurrentDirectory()).Build().Run();
//如果RestartWebserver为false,请退出所有内容。。。
如果(!RestartWebserver)
{
Console.WriteLine(“重新启动…”);
打破
}
//重置要创建的“新”主机。
RestartWebserver=false;
}
}
//用于创建主机的助手,返回要生成和运行的IWebHostBuilder。
公共静态IWebHostBuilder CreateWebHostBuilder(字符串[]参数,字符串ContentRoot)
{
var Address=“0.0.0.0”;
字符串http=“http://”+Address+:“+HttpPort;
//在配置中添加参数(我们也可以在这里设置端口..)
var configuration=new ConfigurationBuilder()
.AddCommandLine(args)
.Build();
Startup.Configuration=配置;
//返回网络主机
返回WebHost.CreateDefaultBuilder(args)
.UseStartup()
.UseContentRoot(ContentRoot)
.UseKestrel()
.useURL(新字符串[]{http}));
}
}
//现在,在任何控制器中,都可以像这样强制关闭主机
公共类重新启动控制器:控制器
{
公共IHostApplicationLifetime\u applicationLifetime;
公共状态控制器(IHostApplicationLifetime applicationLifetime)
{
_applicationLifetime=applicationLifetime;
}
公共异步任务索引()
{
//设置新端口
Program.HttpPort=12345;
//将服务器设置为重新启动
Program.RestartWebserver=true;
//退出当前主机
_applicationLifetime.StopApplication();
//此功能将失败。
//建议创建一个javascript文件,将客户端重新加载到新端口。。。
返回视图(“索引”);
}
}
这个方法对我来说很有效,但是上面的代码有点像sudo,它从未以它的形式编译过。虽然概念已在dotnet v5.0.100上测试和确认这是不可能的,但port属性表示服务器正在通信的端口号。您不能在运行时修改它。我试图实现它,但IApplicationBuilder在“Startup类”的DI other中不可用。那么,在运行时怎么可能做到这一点呢?只需获取HttpContext。您可以访问它的“功能”属性。根据您想要它的位置,它可能已经在基类(如控制器)中可用。在您自己的服务中,您只需注册服务即可;并注入IHttpContextAccessor以访问HttpContext奇怪的是,我无法访问HttpContext.Features.get();也不是HttpContext.Features.Get();在我的控制器里。我认为功能仅限于这些接口: