dotnet 8.0.424 SDK + Microsoft.WindowsAppSDK 2.4.0 实测 build 通过(0 警告 0 错误)。
修复内容:
- csproj:TargetFramework 从 net8.0-windows 改为 net8.0-windows10.0.19041.0,新增 SupportedOSPlatformVersion=10.0.17763.0(WindowsAppSDK 2.4.0 强制要求 TFM 带 Windows 版本号)
- 4 个 Services 文件加 using System.IO(Logger/ConfigService/StateService/GotifyWebSocketService)。WPF+WinForms 双开项目模板不自动 import System.IO
- GotifyApiService 加 using System.Net.Http(同上,HttpClient 不在 implicit usings)
- App.xaml.cs:using Application = System.Windows.Application(WPF/WinForms Application 撞名)
- MainWindow.xaml.cs:using MessageBox = System.Windows.MessageBox(同上,MessageBox 撞名)
- App.xaml.cs:SendTestNotificationAsync 里 Date 从 .UtcNow.ToUnixTimeMilliseconds() 改回 .UtcNow(原 plan 写法有 bug,long 塞不进 DateTimeOffset)
- App.xaml.cs:OnNotificationInvoked 重写:WindowsAppSDK 2.4.0 把 args.Arguments 从 string 改成 IDictionary<string,string>,改用 TryGetValue("url", out var url) 取参数
- NotificationService:AddArgument 从单参 "url=escaped" 改成两参 ("url", url)。WindowsAppSDK 2.4.0 不再支持单参 "key=value" 写法
- GotifyWebSocketService:删除 _forceReconnect 字段(写了不读,改成 RequestReconnect 只调 socket.Abort 让外层 RunLoopAsync 自然重连)
59 lines
2.3 KiB
C#
59 lines
2.3 KiB
C#
using System.Net.Http;
|
|
using System.Net.Http.Headers;
|
|
using System.Text.Json;
|
|
using WindowsGotify.Models;
|
|
|
|
namespace WindowsGotify.Services;
|
|
|
|
public sealed class GotifyApiService
|
|
{
|
|
private readonly GotifyConfig _config;
|
|
private readonly Logger _logger;
|
|
private readonly HttpClient _httpClient;
|
|
|
|
public GotifyApiService(AppConfig config, Logger logger)
|
|
{
|
|
_config = config.Gotify;
|
|
_logger = logger;
|
|
_httpClient = new HttpClient
|
|
{
|
|
BaseAddress = new Uri(_config.Url.TrimEnd('/') + "/"),
|
|
Timeout = TimeSpan.FromSeconds(15)
|
|
};
|
|
|
|
_httpClient.DefaultRequestHeaders.Authorization =
|
|
new AuthenticationHeaderValue("Bearer", _config.ClientToken);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 拉取 lastId 之后的增量消息(去重补齐用)。
|
|
/// </summary>
|
|
public async Task<List<GotifyMessage>> GetMessagesSinceAsync(long sinceId, int limit, CancellationToken ct)
|
|
{
|
|
var path = $"message?limit={Math.Clamp(limit, 1, 100)}&since={Math.Max(0, sinceId)}";
|
|
using var response = await _httpClient.GetAsync(path, ct);
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
await using var stream = await response.Content.ReadAsStreamAsync(ct);
|
|
var page = await JsonSerializer.DeserializeAsync<GotifyMessagePage>(stream, cancellationToken: ct);
|
|
return page?.Messages ?? new List<GotifyMessage>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 拉取最近 N 条消息(历史查看用)。since=0 让服务器从最新往旧返回。
|
|
/// </summary>
|
|
public async Task<List<GotifyMessage>> GetRecentMessagesAsync(int limit, CancellationToken ct)
|
|
{
|
|
var path = $"message?limit={Math.Clamp(limit, 1, 100)}&since=0";
|
|
using var response = await _httpClient.GetAsync(path, ct);
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
await using var stream = await response.Content.ReadAsStreamAsync(ct);
|
|
var page = await JsonSerializer.DeserializeAsync<GotifyMessagePage>(stream, cancellationToken: ct);
|
|
var list = page?.Messages ?? new List<GotifyMessage>();
|
|
|
|
// Gotify 的 since=0 行为:返回数据库里所有消息中的 limit 条;按 id 降序再取一次保证正确
|
|
return list.OrderByDescending(m => m.Id).Take(Math.Clamp(limit, 1, 100)).ToList();
|
|
}
|
|
}
|