From 16c34fbc9692ca20f94ead40af8fc280eb012bee Mon Sep 17 00:00:00 2001 From: xljiulang <366193849@qq.com> Date: Sun, 20 Jun 2021 10:58:27 +0800 Subject: [PATCH] 1.0.0-rc1 --- FastGithub/FastGithub.csproj | 4 +- FastGithub/Program.cs | 6 +- FastGithub/VersionHostedService.cs | 94 ++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 FastGithub/VersionHostedService.cs diff --git a/FastGithub/FastGithub.csproj b/FastGithub/FastGithub.csproj index 958d741..90c518d 100644 --- a/FastGithub/FastGithub.csproj +++ b/FastGithub/FastGithub.csproj @@ -4,7 +4,7 @@ Exe enable net5.0;net6.0 - 1.0.0-beta4 + 1.0.0-rc1 github定制版的dns服务,解析github最优的ip MIT https://github.com/xljiulang/FastGithub @@ -25,7 +25,7 @@ PreserveNewest - PreserveNewest + PreserveNewest diff --git a/FastGithub/Program.cs b/FastGithub/Program.cs index 697213c..829b7e7 100644 --- a/FastGithub/Program.cs +++ b/FastGithub/Program.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; namespace FastGithub { @@ -25,7 +26,8 @@ namespace FastGithub .UseWindowsService() .UseBinaryPathContentRoot() .ConfigureServices((ctx, services) => - { + { + services.AddHostedService(); services.AddGithubDns(ctx.Configuration); }); } diff --git a/FastGithub/VersionHostedService.cs b/FastGithub/VersionHostedService.cs new file mode 100644 index 0000000..f9e6ea2 --- /dev/null +++ b/FastGithub/VersionHostedService.cs @@ -0,0 +1,94 @@ +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.Threading; +using System.Threading.Tasks; + +namespace FastGithub +{ + /// + /// 版本检查 + /// + sealed class VersionHostedService : IHostedService + { + private readonly ILogger logger; + + public VersionHostedService(ILogger logger) + { + this.logger = logger; + } + + public Task StartAsync(CancellationToken cancellationToken) + { + return this.CheckVersionAsync(cancellationToken); + } + + public Task StopAsync(CancellationToken cancellationToken) + { + return Task.CompletedTask; + } + + /// + /// 检测新版本 + /// + /// + /// + private async Task CheckVersionAsync(CancellationToken cancellationToken) + { + try + { + var version = Assembly.GetEntryAssembly()?.GetName().Version; + if (version == null) + { + return; + } + + using var httpClient = new HttpClient(); + var uri = "https://gitee.com/api/v5/repos/jiulang/fast-github/releases?page=1&per_page=1&direction=desc"; + var results = await httpClient.GetFromJsonAsync(uri, cancellationToken); + var release = results?.FirstOrDefault(); + if (release == null) + { + return; + } + + if (string.Equals(release.tag_name, version.ToString(), StringComparison.OrdinalIgnoreCase) == false) + { + this.logger.LogInformation($"您正在使用{version}版本{Environment.NewLine}请前往https://gitee.com/jiulang/fast-github/releases下载新版本"); + this.logger.LogInformation(release.ToString()); + } + } + catch (Exception ex) + { + this.logger.LogWarning($"检测升级信息失败:{ex.Message}"); + } + } + + + /// + /// 发行记录 + /// + private class Release + { + public string tag_name { get; set; } = string.Empty; + + public string body { get; set; } = string.Empty; + + public DateTime created_at { get; set; } + + public override string ToString() + { + return new StringBuilder() + .Append("最新版本:").AppendLine(this.tag_name) + .Append("发布时间:").AppendLine(this.created_at.ToString()) + .AppendLine("更新内容:").AppendLine(this.body) + .ToString(); + } + } + } +}