diff --git a/samples/GarnetWindowsService/GarnetWindowsService.sln b/samples/GarnetWindowsService/GarnetWindowsService.sln new file mode 100644 index 0000000..6a74725 --- /dev/null +++ b/samples/GarnetWindowsService/GarnetWindowsService.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.10.35027.167 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GarnetWindowsService", "GarnetWindowsService\GarnetWindowsService.csproj", "{AA03C1F8-86B1-446F-B4A0-4A69903351BB}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {AA03C1F8-86B1-446F-B4A0-4A69903351BB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AA03C1F8-86B1-446F-B4A0-4A69903351BB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AA03C1F8-86B1-446F-B4A0-4A69903351BB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AA03C1F8-86B1-446F-B4A0-4A69903351BB}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {9293CD78-718B-4A38-BE60-4A1C7DF6D3F7} + EndGlobalSection +EndGlobal diff --git a/samples/GarnetWindowsService/GarnetWindowsService/GarnetService.cs b/samples/GarnetWindowsService/GarnetWindowsService/GarnetService.cs new file mode 100644 index 0000000..596d961 --- /dev/null +++ b/samples/GarnetWindowsService/GarnetWindowsService/GarnetService.cs @@ -0,0 +1,35 @@ +// Copyright (c) HelloShop Corporation. All rights reserved. +// See the license file in the project root for more information. + +using Garnet; + +namespace GarnetWindowsService +{ + public class GarnetService(ILoggerFactory loggerFactory) : BackgroundService + { + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + if (!stoppingToken.IsCancellationRequested) + { + using var server = new GarnetServer([]); + + ILogger logger = loggerFactory.CreateLogger(); + + logger.LogInformation("Starting Garnet server..."); + + try + { + server.Start(); + } + catch (Exception ex) + { + logger.LogError(ex, "An error occurred while starting the Garnet server."); + } + + logger.LogInformation("Garnet server started."); + + await Task.Delay(Timeout.Infinite, stoppingToken); + } + } + } +} diff --git a/samples/GarnetWindowsService/GarnetWindowsService/GarnetWindowsService.csproj b/samples/GarnetWindowsService/GarnetWindowsService/GarnetWindowsService.csproj new file mode 100644 index 0000000..2775120 --- /dev/null +++ b/samples/GarnetWindowsService/GarnetWindowsService/GarnetWindowsService.csproj @@ -0,0 +1,15 @@ + + + + net8.0 + enable + enable + dotnet-GarnetWindowsService-d4bb1a65-ccaf-4675-9048-996583428873 + + + + + + + + diff --git a/samples/GarnetWindowsService/GarnetWindowsService/Program.cs b/samples/GarnetWindowsService/GarnetWindowsService/Program.cs new file mode 100644 index 0000000..9f5bdb1 --- /dev/null +++ b/samples/GarnetWindowsService/GarnetWindowsService/Program.cs @@ -0,0 +1,17 @@ +// Copyright (c) HelloShop Corporation. All rights reserved. +// See the license file in the project root for more information. + +using GarnetWindowsService; + +var builder = Host.CreateApplicationBuilder(args); + +builder.Services.AddWindowsService(options => +{ + options.ServiceName = "Garnet Service"; +}); + +builder.Services.AddHostedService(); + +var host = builder.Build(); + +host.Run(); \ No newline at end of file diff --git a/samples/GarnetWindowsService/GarnetWindowsService/Properties/launchSettings.json b/samples/GarnetWindowsService/GarnetWindowsService/Properties/launchSettings.json new file mode 100644 index 0000000..eb54af3 --- /dev/null +++ b/samples/GarnetWindowsService/GarnetWindowsService/Properties/launchSettings.json @@ -0,0 +1,12 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "profiles": { + "GarnetWindowsService": { + "commandName": "Project", + "dotnetRunMessages": true, + "environmentVariables": { + "DOTNET_ENVIRONMENT": "Development" + } + } + } +} diff --git a/samples/GarnetWindowsService/GarnetWindowsService/appsettings.Development.json b/samples/GarnetWindowsService/GarnetWindowsService/appsettings.Development.json new file mode 100644 index 0000000..b2dcdb6 --- /dev/null +++ b/samples/GarnetWindowsService/GarnetWindowsService/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/samples/GarnetWindowsService/GarnetWindowsService/appsettings.json b/samples/GarnetWindowsService/GarnetWindowsService/appsettings.json new file mode 100644 index 0000000..b2dcdb6 --- /dev/null +++ b/samples/GarnetWindowsService/GarnetWindowsService/appsettings.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/samples/GarnetWindowsService/README.md b/samples/GarnetWindowsService/README.md new file mode 100644 index 0000000..eb79af3 --- /dev/null +++ b/samples/GarnetWindowsService/README.md @@ -0,0 +1,27 @@ +# ʹÓà Windows Service °²Ñb Garnet ·Ö²¼Ê½»º´æ + +Áã¶È¿ò¼ÜʹÓÃ΢Èí Garnet Ìæ´ú Redis ·Ö²¼Ê½»º´æ£¬¸ÃʾÀýÑÝʾÈçºÎʹÓà Windows Service ÔÚµ¥»úÉÏ°²×° Garnet ·þÎñ£¬Éú³ÉÓ¦ÓóÌÐòºóʹÓÃÒÔÏÂÃüÁî²Ù×÷·þÎñ¡£ + +## ´´½¨·þÎñ + +```shell +sc.exe create GarnetService binpath="C:\GarnetWindowsService.exe" start= auto +``` + +## Æô¶¯·þÎñ + +```shell +sc.exe start GarnetService +``` + +## Í£Ö¹·þÎñ + +```shell +sc.exe stop GarnetService +``` + +## жÔØ·þÎñ + +```shell +sc.exe delete GarnetService +``` \ No newline at end of file