5 Star 46 Fork 16

Redis for Windows / Redis for Windows

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Program.cs 2.10 KB
一键复制 编辑 原始数据 按行查看 历史
using System.Diagnostics;
namespace RedisService
{
class Program
{
static void Main(string[] args)
{
string configFilePath = "redis.conf";
if (args.Length > 1 && args[0] == "-c")
{
configFilePath = args[1];
}
IHost host = Host.CreateDefaultBuilder().UseWindowsService().ConfigureServices((hostContext, services) =>
{
services.AddHostedService(serviceProvider =>
new RedisService(configFilePath));
}).Build();
host.Run();
}
}
public class RedisService(string configFilePath) : BackgroundService
{
private Process? redisProcess = new();
public override Task StartAsync(CancellationToken stoppingToken)
{
var basePath = Path.Combine(AppContext.BaseDirectory);
if (!Path.IsPathRooted(configFilePath))
{
configFilePath = Path.Combine(basePath, configFilePath);
}
configFilePath = Path.GetFullPath(configFilePath);
var diskSymbol = configFilePath[..configFilePath.IndexOf(":")];
var fileConf = configFilePath.Replace(diskSymbol + ":", "/cygdrive/" + diskSymbol).Replace("\\", "/");
string fileName = Path.Combine(basePath, "redis-server.exe").Replace("\\", "/");
string arguments = $"\"{fileConf}\"";
ProcessStartInfo processStartInfo = new(fileName, arguments)
{
WorkingDirectory = basePath
};
redisProcess = Process.Start(processStartInfo);
return Task.CompletedTask;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await Task.Delay(-1, stoppingToken);
}
public override Task StopAsync(CancellationToken stoppingToken)
{
if (redisProcess != null)
{
redisProcess.Kill();
redisProcess.Dispose();
}
return Task.CompletedTask;
}
}
}
1
https://gitee.com/redis-windows/redis-windows.git
git@gitee.com:redis-windows/redis-windows.git
redis-windows
redis-windows
Redis for Windows
main

搜索帮助