mirror of
https://github.com/FastTunnel/FastTunnel.git
synced 2025-02-08 02:39:29 +08:00
使用net core官方的后台服务运行方式
This commit is contained in:
parent
316cd40839
commit
4db2003c4b
|
@ -1,4 +1,4 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
|
|
|
@ -11,81 +11,44 @@ using System.Threading;
|
|||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using NLog.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NLog;
|
||||
using FastTunnel.Core.Config;
|
||||
using FastTunnel.Core.Host;
|
||||
using FastTunnel.Core.Core;
|
||||
using FastTunnel.Core.Handlers;
|
||||
using FastTunnel.Core.Handlers.Server;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using FastTunnel.Server.Service;
|
||||
using FastTunnel.Core.Logger;
|
||||
|
||||
namespace FastTunnel.Server
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static Appsettings appsettings;
|
||||
|
||||
static void Main(string[] args)
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
LogManager.Configuration = NlogConfig.getNewConfig();
|
||||
var logger = LogManager.GetCurrentClassLogger();
|
||||
|
||||
logger.Debug("===== FastTunnel Server Start =====");
|
||||
logger.Debug("===== args =====" + Environment.NewLine + string.Join(Environment.NewLine, args));
|
||||
|
||||
try
|
||||
{
|
||||
var servicesProvider = new Host().Config(Config).Build();
|
||||
Run(servicesProvider);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// NLog: catch any exception and log it.
|
||||
logger.Error(ex);
|
||||
Console.WriteLine(ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
|
||||
LogManager.Shutdown();
|
||||
}
|
||||
CreateHostBuilder(args).Build().Run();
|
||||
}
|
||||
|
||||
private static ServerConfig implementationFactory(IServiceProvider arg)
|
||||
{
|
||||
if (appsettings == null)
|
||||
{
|
||||
var conf = new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.json", true, true)
|
||||
.Build();
|
||||
/// <summary>
|
||||
/// 使用托管服务实现后台任务:https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-5.0&tabs=visual-studio
|
||||
/// </summary>
|
||||
/// <param name="args"></param>
|
||||
/// <returns></returns>
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
Host.CreateDefaultBuilder(args)
|
||||
.ConfigureLogging((context) =>
|
||||
{
|
||||
context.AddNLog(NlogConfig.getNewConfig());
|
||||
})
|
||||
.ConfigureServices((hostContext, services) =>
|
||||
{
|
||||
services.AddHostedService<ServiceFastTunnelServer>();
|
||||
|
||||
appsettings = conf.Get<Appsettings>();
|
||||
}
|
||||
|
||||
return appsettings.ServerSettings;
|
||||
}
|
||||
|
||||
private static void Config(ServiceCollection service)
|
||||
{
|
||||
service.AddSingleton<FastTunnelServer>()
|
||||
.AddSingleton<LoginHandler>()
|
||||
.AddSingleton<HeartHandler>()
|
||||
.AddSingleton<SwapMsgHandler>()
|
||||
.AddSingleton<IConfigHandler, ConfigHandler>()
|
||||
.AddSingleton<ServerConfig>(implementationFactory);
|
||||
}
|
||||
|
||||
private static void Run(IServiceProvider servicesProvider)
|
||||
{
|
||||
var server = servicesProvider.GetRequiredService<FastTunnelServer>();
|
||||
server.Run();
|
||||
|
||||
while (true)
|
||||
{
|
||||
Thread.Sleep(10000 * 60);
|
||||
}
|
||||
}
|
||||
// DI
|
||||
services.AddTransient<FastTunnelServer>();
|
||||
services.AddTransient<LoginHandler>();
|
||||
services.AddTransient<HeartHandler>();
|
||||
services.AddTransient<SwapMsgHandler>();
|
||||
services.AddTransient<IConfigHandler, ConfigHandler>();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
55
FastTunnel.Server/Service/ServiceFastTunnelServer.cs
Normal file
55
FastTunnel.Server/Service/ServiceFastTunnelServer.cs
Normal file
|
@ -0,0 +1,55 @@
|
|||
using FastTunnel.Core.Core;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FastTunnel.Server.Service
|
||||
{
|
||||
public class ServiceFastTunnelServer : IHostedService
|
||||
{
|
||||
ILogger<ServiceFastTunnelServer> _logger;
|
||||
IConfiguration _configuration;
|
||||
FastTunnelServer _fastTunnelServer;
|
||||
|
||||
public ServiceFastTunnelServer(ILogger<ServiceFastTunnelServer> logger, IConfiguration configuration, FastTunnelServer fastTunnelServer)
|
||||
{
|
||||
_logger = logger;
|
||||
_configuration = configuration;
|
||||
_fastTunnelServer = fastTunnelServer;
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("===== FastTunnel Server Starting =====");
|
||||
|
||||
try
|
||||
{
|
||||
Run();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// NLog: catch any exception and log it.
|
||||
_logger.LogError(ex, "Server Error");
|
||||
Console.WriteLine(ex);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private void Run()
|
||||
{
|
||||
_fastTunnelServer.Run(_configuration.Get<Appsettings>().ServerSettings);
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("===== FastTunnel Server Stoping =====");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +0,0 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<ShowAllFiles>true</ShowAllFiles>
|
||||
</PropertyGroup>
|
||||
</Project>
|
|
@ -2,8 +2,8 @@
|
|||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
"Microsoft": "Error",
|
||||
"Microsoft.Hosting.Lifetime": "Error"
|
||||
}
|
||||
},
|
||||
"ServerSettings": {
|
||||
|
|
Loading…
Reference in New Issue
Block a user