我有这个 workerservice,它本质上是一个 TCP 套接字服务器。
这是我主要选择根据操作系统设置服务的地方。
public static void Main(string[] args)
{
Log log = new();
Socket socket = new SocketConnection().getSocket();
try
{
DataStorage.Instance.osPlatform = RuntimeInformation.OSDescription;
if (DataStorage.Instance.osPlatform.Contains("Win"))
{
Console.WriteLine("WinOS");
CreateHostBuilderWin(args).Build().Run();
}
else
{
Console.WriteLine("MacOS");
CreateHostBuilderMac(args).Build().Run();
}
}
catch (Exception ex)
{
log.write("Could not start SDS. ", ex);
}
log.write("Program is reached on shutdown", null);
}
public static void configureServices(HostBuilderContext context, IServiceCollection services)
{
services.AddHostedService<Worker>();
}
public static IHostBuilder CreateHostBuilderWin(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseWindowsService()
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Worker>();
});
public static IHostBuilder CreateHostBuilderMac(string[] args) =>
Host.CreateDefaultBuilder(args)
//Hvorfor dette?
.ConfigureServices(configureServices);
}
这是 Worker,我在其中以套接字作为参数启动 clientServer。这就是问题所在。
我似乎无法正确关闭服务。我初始化并实例化了一个 Socket,我想在服务停止或崩溃时处理它。
处置发生在 EndSDS 类中
using Microsoft.Extensions.Hosting;
using SDS.BusinessLogic;
using SDS.Singleton;
using System;
using System.Threading;
using System.Threading.Tasks;
using SDS.Service.Tools;
using System.Net.Sockets;
namespace SDS
{
public class Worker : BackgroundService
{
Log log = new();
ClientServer clientServer;
SocketConnection socket = new();
Socket listener;
EndSDS endSDS;
private readonly IHostApplicationLifetime _hostApplicationLifetime;
public Worker(IHostApplicationLifetime hostApplicationLifetime)
{
_hostApplicationLifetime = hostApplicationLifetime;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
try{
while (!stoppingToken.IsCancellationRequested)
{
try
{
// hvis pludseligt det går galt med initialiseringen så skal SDS stoppe
// ved næste gennemløb
if (DataStorage.Instance.sdsInitialized)
{
log.write("startasync is reached", null);
listener = socket.getSocket();
log.write("Socket: " + listener.AddressFamily + " " + listener.RemoteEndPoint, null);
await clientServer.start(listener);
}
else
{
log.write("Sds not initialized properly", null);
}
}
catch (Exception ex)
{
log.write("Could not create Socket for server", ex);
}
}
}
finally
{
log.write("Finally reached", null);
_hostApplicationLifetime.StopApplication();
}
}
//log.write("Worker is reached on shutdown", null);
public override Task StartAsync(CancellationToken cancellationToken)
{
log.write("StartAsync is reached on startup", null);
clientServer = new();
return base.StartAsync(cancellationToken);
}
public override Task StopAsync(CancellationToken cancellationToken)
{
log.write("StopAsync is reached on shutdown", null);
endSDS = new EndSDS();
endSDS.start(listener);
return base.StopAsync(cancellationToken);
}
/* public override void Dispose()
{
log.write("Dispose is reached on shutdown", null);
endSDS.start(listener);
base.Dispose();
}*/
}
}
这是 EndSDS 类。
using SDS.Controllers;
using SDS.Singleton;
using SDS.Service.Tools;
using System.Net.Sockets;
using System;
namespace SDS.BusinessLogic
{
internal class EndSDS
{
private FilesController files = new();
private Log log = new();
internal EndSDS()
{
}
internal void start(Socket socket)
{
log.write("socket closing", null);
if (socket != null)
{
try
{
socket.Close();
socket.Dispose();
}
catch (Exception ex)
{
log.write("logclose exception. ", ex);
};
log.write("Socket Disposed", null);
}
log.write("SDS ended. ", null);
}
}
}
会发生什么。
我启动了服务,它正常启动,因为我可以连接到它并与之交谈。我还可以在日志中看到“启动时达到 StartAsync”
我停止了服务,并且 StopAsync 没有运行。什么都没发生。日志不会写入日志。
我启动了服务,但它没有启动 clientServer,因为端口没有关闭,我在日志中收到一条消息。
我停止服务,StopAsync 运行,一切正常。
冲洗并重复。
任何人都可以帮助我吗?,我不知道问题是什么。
感谢您的时间。