From 3628729a931550ee5b66870ac58d8346fb8e245e Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 28 Oct 2021 15:47:47 +0800 Subject: [PATCH] feature: supports build with .NET 6 --- build.bat | 14 ++++++++++++++ src/Models/Issue.cs | 17 +++++++++++++++-- src/Models/Version.cs | 21 +++++++++++++++++---- src/SourceGit.csproj | 2 +- src/Views/Controls/Avatar.cs | 27 +++++++++++++++++++++++++++ 5 files changed, 74 insertions(+), 7 deletions(-) diff --git a/build.bat b/build.bat index 02927c0e..bfa9479b 100644 --- a/build.bat +++ b/build.bat @@ -25,4 +25,18 @@ dotnet publish SourceGit.csproj --nologo -c Release -r win-x64 -f net5.0-windows cd ..\publish ren SourceGit.exe SourceGit.net5.0.x64.exe +cd ..\src +rmdir /s /q bin +rmdir /s /q obj +dotnet publish SourceGit.csproj --nologo -c Release -r win-x86 -f net6.0-windows -p:PublishSingleFile=true --no-self-contained -o ..\publish +cd ..\publish +ren SourceGit.exe SourceGit.net6.0.x86.exe + +cd ..\src +rmdir /s /q bin +rmdir /s /q obj +dotnet publish SourceGit.csproj --nologo -c Release -r win-x64 -f net6.0-windows -p:PublishSingleFile=true --no-self-contained -o ..\publish +cd ..\publish +ren SourceGit.exe SourceGit.net6.0.x64.exe + cd ../ \ No newline at end of file diff --git a/src/Models/Issue.cs b/src/Models/Issue.cs index 2bdc6e29..dc6b2528 100644 --- a/src/Models/Issue.cs +++ b/src/Models/Issue.cs @@ -1,10 +1,15 @@ using System; -using System.Net; using System.Reflection; -using System.Text; using System.Text.Json; using System.Text.Json.Serialization; +#if NET6_0_OR_GREATER +using System.Net.Http; +#else +using System.Net; +using System.Text; +#endif + namespace SourceGit.Models { /// /// 崩溃日志上报 @@ -40,6 +45,13 @@ namespace SourceGit.Models { e.Source, e.StackTrace); +#if NET6_0_OR_GREATER + var req = new HttpClient(); + req.DefaultRequestHeaders.Add("Content-Type", "application/json"); + req.DefaultRequestHeaders.Add("charset", "UTF-8"); + req.Timeout = TimeSpan.FromSeconds(1); + req.PostAsync("https://gitee.com/api/v5/repos/sourcegit/issues", new StringContent(JsonSerializer.Serialize(issue))).Wait(); +#else var req = WebRequest.CreateHttp("https://gitee.com/api/v5/repos/sourcegit/issues"); req.Method = "POST"; req.ContentType = "application/json"; @@ -53,6 +65,7 @@ namespace SourceGit.Models { } req.GetResponse(); +#endif } catch { } } } diff --git a/src/Models/Version.cs b/src/Models/Version.cs index 07e384e9..1402b1c8 100644 --- a/src/Models/Version.cs +++ b/src/Models/Version.cs @@ -1,12 +1,17 @@ using System; -using System.Net; using System.Reflection; -using System.Text; using System.Text.Json; using System.Text.Json.Serialization; using System.Text.RegularExpressions; using System.Threading.Tasks; +#if NET6_0_OR_GREATER +using System.Net.Http; +#else +using System.Net; +using System.Text; +#endif + namespace SourceGit.Models { /// @@ -43,10 +48,18 @@ namespace SourceGit.Models { var lastDayOfYear = Preference.Instance.General.LastCheckDay; if (lastDayOfYear != curDayOfYear) { Preference.Instance.General.LastCheckDay = curDayOfYear; - Task.Run(() => { + Task.Run(async () => { try { +#if NET6_0_OR_GREATER + var req = new HttpClient(); + var rsp = await req.GetAsync("https://gitee.com/api/v5/repos/sourcegit/sourcegit/releases/latest"); + rsp.EnsureSuccessStatusCode(); + + var raw = await rsp.Content.ReadAsStringAsync(); +#else var web = new WebClient() { Encoding = Encoding.UTF8 }; - var raw = web.DownloadString("https://gitee.com/api/v5/repos/sourcegit/sourcegit/releases/latest"); + var raw = await web.DownloadStringTaskAsync("https://gitee.com/api/v5/repos/sourcegit/sourcegit/releases/latest"); +#endif var ver = JsonSerializer.Deserialize(raw); var cur = Assembly.GetExecutingAssembly().GetName().Version; diff --git a/src/SourceGit.csproj b/src/SourceGit.csproj index 88c88b67..6ecf9544 100644 --- a/src/SourceGit.csproj +++ b/src/SourceGit.csproj @@ -1,6 +1,6 @@ - net5.0-windows;net48 + net6.0-windows;net5.0-windows;net48 WinExe true App.ico diff --git a/src/Views/Controls/Avatar.cs b/src/Views/Controls/Avatar.cs index 70a785e4..3043850e 100644 --- a/src/Views/Controls/Avatar.cs +++ b/src/Views/Controls/Avatar.cs @@ -11,6 +11,10 @@ using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Imaging; +#if NET6_0_OR_GREATER +using System.Net.Http; +#endif + namespace SourceGit.Views.Controls { /// @@ -192,6 +196,28 @@ namespace SourceGit.Views.Controls { if (!requesting.ContainsKey(email)) return; try { +#if NET6_0_OR_GREATER + var req = new HttpClient().GetAsync(Models.Preference.Instance.General.AvatarServer + md5 + "?d=404"); + req.Wait(); + + var rsp = req.Result; + if (rsp != null && rsp.StatusCode == HttpStatusCode.OK) { + var writer = File.OpenWrite(filePath); + rsp.Content.CopyToAsync(writer).Wait(); + writer.Close(); + + a.Dispatcher.Invoke(() => { + var img = new BitmapImage(new Uri(filePath)); + loaded.Add(email, img); + + if (requesting.ContainsKey(email)) { + foreach (var one in requesting[email]) one.Source = img; + } + }); + } else { + if (!loaded.ContainsKey(email)) loaded.Add(email, null); + } +#else HttpWebRequest req = WebRequest.CreateHttp(Models.Preference.Instance.General.AvatarServer + md5 + "?d=404"); req.Timeout = 2000; req.Method = "GET"; @@ -214,6 +240,7 @@ namespace SourceGit.Views.Controls { } else { if (!loaded.ContainsKey(email)) loaded.Add(email, null); } +#endif } catch { if (!loaded.ContainsKey(email)) loaded.Add(email, null); }