【发布时间】:2017-09-07 04:30:39
【问题描述】:
我在删除服务结构应用程序时看到了这一点。 应用程序内部服务的旧进程仍在运行。
应用中包含的服务类型
演员
无状态服务
ASP.NET 核心
我在重新部署应用程序时注意到了这个问题,并且 ASP.NET Core 服务为“EADDRINUSE 地址已在使用中”抛出错误
是否有适当的方法来彻底删除停止服务的所有内部进程的应用程序?
是否有任何可以在流程中捕获的取消事件?
我正在使用以下类将其注册为无状态服务。
/// <summary>
/// A specialized stateless service for hosting ASP.NET Core web apps.
/// </summary>
internal sealed class WebHostingService : StatelessService, ICommunicationListener
{
private readonly string _endpointName;
private IWebHost _webHost;
public WebHostingService(StatelessServiceContext serviceContext, string endpointName)
: base(serviceContext)
{
_endpointName = endpointName;
}
#region StatelessService
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
return new[] { new ServiceInstanceListener(_ => this) };
}
#endregion StatelessService
#region ICommunicationListener
void ICommunicationListener.Abort()
{
_webHost?.Dispose();
}
Task ICommunicationListener.CloseAsync(CancellationToken cancellationToken)
{
_webHost?.Dispose();
return Task.FromResult(true);
}
Task<string> ICommunicationListener.OpenAsync(CancellationToken cancellationToken)
{
var endpoint = FabricRuntime.GetActivationContext().GetEndpoint(_endpointName);
string serverUrl = $"{endpoint.Protocol}://{FabricRuntime.GetNodeContext().IPAddressOrFQDN}:{endpoint.Port}";
_webHost = new WebHostBuilder().UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseUrls(serverUrl)
.Build();
_webHost.Start();
return Task.FromResult(serverUrl);
}
#endregion ICommunicationListener
}
并像这样注册它
ServiceRuntime.RegisterServiceAsync("WebApiType", context => new WebHostingService(context, "ServiceEndpoint")).GetAwaiter().GetResult();
Thread.Sleep(Timeout.Infinite);
【问题讨论】:
-
是的,它叫RunAsync。
-
你看到这篇关于托管 aspnet 核心的文章了吗? docs.microsoft.com/en-us/azure/service-fabric/… 如果您使用 'AspNetCoreCommunicationListener' 可能会有所帮助
-
@LoekD 确实查看了这篇文章。更新为使用 WebListenerCommunicationListener。但我仍然收到错误 “前缀 'http://+:8800/' 已注册。” 在升级或删除和安装时。
标签: c# asp.net azure-service-fabric