Skip to content
MeshLab
All posts

Deploy Garnet as Windows Service (Redis Alternative)

Mesh
  1. Create a Worker Service Project
  2. Install Microsoft.Extensions.Hosting.WindowsServices and obviously Microsoft.Garnet.
  3. Add this to your Program.cs:
builder.Services.AddWindowsService(options =>
{
    options.ServiceName = "Garnet Cache";
 });
  1. Inside Worker class, remove the code in the ExecuteAsync method, and replace it with:
if (!stoppingToken.IsCancellationRequested)
{
    using var server = new GarnetServer(options);
    server.Start();
    await Task.Delay(Timeout.Infinite, stoppingToken);
}
  1. Add this to the first PropertyGroup in your .csproj file:
<PublishSingleFile>true</PublishSingleFile>
<DebugType>none</DebugType>
<PublishReadyToRun>true</PublishReadyToRun>
<SelfContained>false</SelfContained>
  1. Run dotnet publish -o output/path
  2. 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

Comments 0

Sign in to join the conversation.

No comments yet. Start the conversation.