Deploy Garnet as Windows Service (Redis Alternative)
- Create a Worker Service Project
- Install
Microsoft.Extensions.Hosting.WindowsServicesand obviouslyMicrosoft.Garnet. - Add this to your
Program.cs:
builder.Services.AddWindowsService(options =>
{
options.ServiceName = "Garnet Cache";
});
- Inside
Workerclass, remove the code in theExecuteAsyncmethod, and replace it with:
if (!stoppingToken.IsCancellationRequested)
{
using var server = new GarnetServer(options);
server.Start();
await Task.Delay(Timeout.Infinite, stoppingToken);
}
- Add this to the first
PropertyGroupin your.csprojfile:
<PublishSingleFile>true</PublishSingleFile>
<DebugType>none</DebugType>
<PublishReadyToRun>true</PublishReadyToRun>
<SelfContained>false</SelfContained>
- Run
dotnet publish -o output/path - Run this powershell:
$exePath = "${PSScriptRoot}\GarnetWorker.exe"
$serviceParameters = @{
Name = "garnet"
BinaryPathName = "${exePath}"
DisplayName = "Garnet"
DependsOn = "TCPIP"
StartupType = "Automatic"
Description = "Garnet cache server service"
}
New-Service @serviceParameters
Start-Service -Name "garnet"
obviously don't forget to change the paths
