diff --git a/FastGithub.Upgrade/ProductionVersion.cs b/FastGithub.Upgrade/ProductionVersion.cs
index c5904b8..58caa1e 100644
--- a/FastGithub.Upgrade/ProductionVersion.cs
+++ b/FastGithub.Upgrade/ProductionVersion.cs
@@ -1,4 +1,5 @@
using System;
+using System.Reflection;
using System.Text.RegularExpressions;
namespace FastGithub.Upgrade
@@ -87,5 +88,20 @@ namespace FastGithub.Upgrade
var subVersion = productionVersion[verion.Length..];
return new ProductionVersion(Version.Parse(verion), subVersion);
}
+
+
+ ///
+ /// 获取当前应用程序的产品版本
+ ///
+ ///
+ public static ProductionVersion? GetApplicationVersion()
+ {
+ var version = Assembly
+ .GetEntryAssembly()?
+ .GetCustomAttribute()?
+ .InformationalVersion;
+
+ return version == null ? null : ProductionVersion.Parse(version);
+ }
}
}
diff --git a/FastGithub.Upgrade/Release.cs b/FastGithub.Upgrade/Release.cs
new file mode 100644
index 0000000..d4f9630
--- /dev/null
+++ b/FastGithub.Upgrade/Release.cs
@@ -0,0 +1,50 @@
+using System;
+using System.Text;
+using System.Text.Json.Serialization;
+
+namespace FastGithub.Upgrade
+{
+ ///
+ /// 发行记录
+ ///
+ sealed class Release
+ {
+ ///
+ /// 标签名
+ ///
+ [JsonPropertyName("tag_name")]
+ public string TagName { get; set; } = string.Empty;
+
+ ///
+ /// 发行说明
+ ///
+ [JsonPropertyName("body")]
+ public string Body { get; set; } = string.Empty;
+
+ ///
+ /// 发行时间
+ ///
+
+ [JsonPropertyName("created_at")]
+ public DateTime CreatedAt { get; set; }
+
+
+ ///
+ /// 获取产品版本
+ ///
+ ///
+ public ProductionVersion GetProductionVersion()
+ {
+ return ProductionVersion.Parse(this.TagName);
+ }
+
+ public override string ToString()
+ {
+ return new StringBuilder()
+ .Append("最新版本:").AppendLine(this.TagName)
+ .Append("发布时间:").AppendLine(this.CreatedAt.ToString())
+ .AppendLine("更新内容:").AppendLine(this.Body)
+ .ToString();
+ }
+ }
+}
diff --git a/FastGithub.Upgrade/UpgradeHostedService.cs b/FastGithub.Upgrade/UpgradeHostedService.cs
index b6bf811..eb52f0f 100644
--- a/FastGithub.Upgrade/UpgradeHostedService.cs
+++ b/FastGithub.Upgrade/UpgradeHostedService.cs
@@ -1,28 +1,30 @@
-using Microsoft.Extensions.Hosting;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
-using System.Linq;
-using System.Net.Http;
-using System.Net.Http.Json;
-using System.Reflection;
-using System.Text;
-using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
namespace FastGithub.Upgrade
{
///
- /// 升级检查后台服务
+ /// 升级后台服务
///
sealed class UpgradeHostedService : IHostedService
{
+ private readonly IServiceScopeFactory serviceScopeFactory;
private readonly ILogger logger;
- private const string DownloadPage = "https://gitee.com/jiulang/fast-github/releases";
- private const string ReleasesUri = "https://gitee.com/api/v5/repos/jiulang/fast-github/releases?page=1&per_page=1&direction=desc";
- public UpgradeHostedService(ILogger logger)
+ ///
+ /// 升级后台服务
+ ///
+ ///
+ ///
+ public UpgradeHostedService(
+ IServiceScopeFactory serviceScopeFactory,
+ ILogger logger)
{
+ this.serviceScopeFactory = serviceScopeFactory;
this.logger = logger;
}
@@ -35,28 +37,13 @@ namespace FastGithub.Upgrade
{
try
{
- var currentVersion = GetCurrentVersion();
- if (currentVersion == null)
- {
- return;
- }
-
- var lastRelease = await GetLastedReleaseAsync(cancellationToken);
- if (lastRelease == null)
- {
- return;
- }
-
- var lastedVersion = ProductionVersion.Parse(lastRelease.TagName);
- if (lastedVersion.CompareTo(currentVersion) > 0)
- {
- this.logger.LogInformation($"您正在使用{currentVersion}版本{Environment.NewLine}请前往{DownloadPage}下载新版本");
- this.logger.LogInformation(lastRelease.ToString());
- }
+ using var scope = this.serviceScopeFactory.CreateScope();
+ var upgradeService = scope.ServiceProvider.GetRequiredService();
+ await upgradeService.UpgradeAsync(cancellationToken);
}
catch (Exception ex)
{
- this.logger.LogWarning($"检测升级信息失败:{ex.Message}");
+ this.logger.LogWarning($"升级失败:{ex.Message}");
}
}
@@ -64,56 +51,5 @@ namespace FastGithub.Upgrade
{
return Task.CompletedTask;
}
-
- ///
- /// 获取当前版本
- ///
- ///
- private static ProductionVersion? GetCurrentVersion()
- {
- var version = Assembly
- .GetEntryAssembly()?
- .GetCustomAttribute()?
- .InformationalVersion;
-
- return version == null ? null : ProductionVersion.Parse(version);
- }
-
- ///
- /// 获取最新发布
- ///
- ///
- private static async Task GetLastedReleaseAsync(CancellationToken cancellationToken)
- {
- using var httpClient = new HttpClient();
- var releases = await httpClient.GetFromJsonAsync(ReleasesUri, cancellationToken);
- return releases?.FirstOrDefault();
- }
-
- ///
- /// 发行记录
- ///
- private class Release
- {
- [JsonPropertyName("tag_name")]
- public string TagName { get; set; } = string.Empty;
-
-
- [JsonPropertyName("body")]
- public string Body { get; set; } = string.Empty;
-
-
- [JsonPropertyName("created_at")]
- public DateTime CreatedAt { get; set; }
-
- public override string ToString()
- {
- return new StringBuilder()
- .Append("最新版本:").AppendLine(this.TagName)
- .Append("发布时间:").AppendLine(this.CreatedAt.ToString())
- .AppendLine("更新内容:").AppendLine(this.Body)
- .ToString();
- }
- }
}
}
diff --git a/FastGithub.Upgrade/UpgradeService.cs b/FastGithub.Upgrade/UpgradeService.cs
new file mode 100644
index 0000000..8ff0d2c
--- /dev/null
+++ b/FastGithub.Upgrade/UpgradeService.cs
@@ -0,0 +1,67 @@
+using Microsoft.Extensions.Logging;
+using System;
+using System.Linq;
+using System.Net.Http;
+using System.Net.Http.Json;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace FastGithub.Upgrade
+{
+ ///
+ /// 升级服务
+ ///
+ sealed class UpgradeService
+ {
+ private readonly ILogger logger;
+ private const string DownloadPage = "https://gitee.com/jiulang/fast-github/releases";
+ private const string ReleasesUri = "https://gitee.com/api/v5/repos/jiulang/fast-github/releases?page=1&per_page=1&direction=desc";
+
+ ///
+ /// 升级服务
+ ///
+ ///
+ public UpgradeService(ILogger logger)
+ {
+ this.logger = logger;
+ }
+
+ ///
+ /// 进行升级
+ ///
+ ///
+ ///
+ public async Task UpgradeAsync(CancellationToken cancellationToken)
+ {
+ var currentVersion = ProductionVersion.GetApplicationVersion();
+ if (currentVersion == null)
+ {
+ return;
+ }
+
+ var lastRelease = await this.GetLastedReleaseAsync(cancellationToken);
+ if (lastRelease == null)
+ {
+ return;
+ }
+
+ var lastedVersion = lastRelease.GetProductionVersion();
+ if (lastedVersion.CompareTo(currentVersion) > 0)
+ {
+ this.logger.LogInformation($"您正在使用{currentVersion}版本{Environment.NewLine}请前往{DownloadPage}下载新版本");
+ this.logger.LogInformation(lastRelease.ToString());
+ }
+ }
+
+ ///
+ /// 获取最新发布
+ ///
+ ///
+ public async Task GetLastedReleaseAsync(CancellationToken cancellationToken)
+ {
+ using var httpClient = new HttpClient();
+ var releases = await httpClient.GetFromJsonAsync(ReleasesUri, cancellationToken);
+ return releases?.FirstOrDefault();
+ }
+ }
+}
diff --git a/FastGithub.Upgrade/UpgradeServiceCollectionExtensions.cs b/FastGithub.Upgrade/UpgradeServiceCollectionExtensions.cs
index 484d128..6d846ad 100644
--- a/FastGithub.Upgrade/UpgradeServiceCollectionExtensions.cs
+++ b/FastGithub.Upgrade/UpgradeServiceCollectionExtensions.cs
@@ -1,5 +1,4 @@
using FastGithub.Upgrade;
-using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace FastGithub
@@ -16,7 +15,9 @@ namespace FastGithub
///
public static IServiceCollection AddAppUpgrade(this IServiceCollection services)
{
- return services.AddHostedService();
+ return services
+ .AddTransient()
+ .AddHostedService();
}
}
}