This commit is contained in:
hello 2024-07-11 20:58:08 +08:00
commit 46a626a97e
8 changed files with 147 additions and 0 deletions

View File

@ -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

View File

@ -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<GarnetService>();
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);
}
}
}
}

View File

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>dotnet-GarnetWindowsService-d4bb1a65-ccaf-4675-9048-996583428873</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="8.0.0" />
<PackageReference Include="Microsoft.Garnet" Version="1.0.15" />
</ItemGroup>
</Project>

View File

@ -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<GarnetService>();
var host = builder.Build();
host.Run();

View File

@ -0,0 +1,12 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"GarnetWindowsService": {
"commandName": "Project",
"dotnetRunMessages": true,
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development"
}
}
}
}

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}

View File

@ -0,0 +1,27 @@
# 使用 Windows Service 安裝 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
```