【发布时间】:2022-01-04 11:36:25
【问题描述】:
我有一个用作 REST Web 服务器的 C# .net core 3.1 控制台应用程序。它作为 Windows 服务运行。它运行良好,但是在我关闭应用程序(或在某些操作中关闭)后,我无法立即启动应用程序。我收到以下错误,
Unable to start Kestrel. System.IO.IOException: Failed to bind to address http://[::]:8000: address already in use.
---> Microsoft.AspNetCore.Connections.AddressInUseException: Only one usage of each socket address(protocol/network/address/port) is normally permitted.
--->System.Net.Sockets.SocketException(10048): Only one usage of each socket address(protocol/network/address/port) is normally permitted.
.
.
.
如果我在大约 5 分钟后尝试,我可以启动应用程序。在我进入应用程序之前,我应该做些什么或可以做些什么来释放端口上的绑定?
#更新 - 1 我使用以下方法在错误等情况下关闭应用程序,
_hostLifetime.StopApplication();
这是初始化网络服务器的代码,
webBuilder.UseStartup<Startup>().UseUrls(restPort.Value);
Startup类的构造函数如下,
_httpListener = new HttpListener();
Startup类中的Configure方法代码如下,
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
【问题讨论】:
-
你能展示管理网络服务器的代码吗?
-
你能指定
shutdown吗? -
“无法找到地址” - “通常不允许” - 这些是实际的错误消息、拼写错误等等吗?
-
"exist" - 可能是另一个错字。猜猜它是“出口”。
-
@vernou 我已经用更多信息更新了这个问题
标签: c# windows rest .net-core-3.1