From 2e647b49bfdfd4f29b23f445ccee725740f52e73 Mon Sep 17 00:00:00 2001 From: Aigio Liu Date: Mon, 17 May 2021 23:36:10 +0800 Subject: [PATCH] lazy http SteamService --- .../Implementation/SteamServiceImpl.cs | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/ST.Client.Desktop/Services/Implementation/SteamServiceImpl.cs b/src/ST.Client.Desktop/Services/Implementation/SteamServiceImpl.cs index f088677c8..71e4a20fd 100644 --- a/src/ST.Client.Desktop/Services/Implementation/SteamServiceImpl.cs +++ b/src/ST.Client.Desktop/Services/Implementation/SteamServiceImpl.cs @@ -41,12 +41,12 @@ namespace System.Application.Services.Implementation readonly string? mSteamDirPath; readonly string? mSteamProgramPath; readonly string[] steamProcess = new[] { "steam", "steamservice", "steamwebhelper" }; - readonly IHttpService http; + readonly Lazy _http = new(() => DI.Get()); + IHttpService Http => _http.Value; - public SteamServiceImpl(IDesktopPlatformService platformService, IHttpService http) + public SteamServiceImpl(IDesktopPlatformService platformService) { this.platformService = platformService; - this.http = http; mSteamDirPath = platformService.GetSteamDirPath(); mSteamProgramPath = platformService.GetSteamProgramPath(); UserVdfPath = SteamDirPath == null ? null : Path.Combine(SteamDirPath, "config", "loginusers.vdf"); @@ -368,7 +368,7 @@ namespace System.Application.Services.Implementation _ => throw new ArgumentOutOfRangeException(nameof(type), type, null), }; if (url == null) return string.Empty; - var value = await http.GetImageAsync(url, ImageChannelType.SteamGames); + var value = await Http.GetImageAsync(url, ImageChannelType.SteamGames); return value ?? string.Empty; } @@ -430,12 +430,12 @@ namespace System.Application.Services.Implementation { try { - var client = http.Factory.CreateClient(); + var client = Http.Factory.CreateClient(); client.Timeout = TimeSpan.FromSeconds(.85); var request = new HttpRequestMessage(HttpMethod.Get, url_localhost_auth_public); request.Headers.Add("Origin", url_store_steampowered); request.Headers.Add("Accept", MediaTypeNames.JSON); - request.Headers.UserAgent.ParseAdd(http.PlatformHelper.UserAgent); + request.Headers.UserAgent.ParseAdd(Http.PlatformHelper.UserAgent); var response = await client.SendAsync(request); #if DEBUG Console.WriteLine("GetLoginUsingSteamClientAuthAsync"); @@ -493,10 +493,10 @@ namespace System.Application.Services.Implementation { try { - var client = http.Factory.CreateClient(); + var client = Http.Factory.CreateClient(); client.Timeout = TimeSpan.FromSeconds(url_steamcommunity_timeout_s); var request = new HttpRequestMessage(HttpMethod.Get, url_store_steampowered); - request.Headers.UserAgent.ParseAdd(http.PlatformHelper.UserAgent); + request.Headers.UserAgent.ParseAdd(Http.PlatformHelper.UserAgent); var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); return response.IsSuccessStatusCode; } @@ -528,8 +528,8 @@ namespace System.Application.Services.Implementation }; request.Headers.Add("Origin", url_store_steampowered); request.Headers.Add("Accept", MediaTypeNames.JSON); - request.Headers.UserAgent.ParseAdd(http.PlatformHelper.UserAgent); - var client = http.Factory.CreateClient(); + request.Headers.UserAgent.ParseAdd(Http.PlatformHelper.UserAgent); + var client = Http.Factory.CreateClient(); client.Timeout = TimeSpan.FromSeconds(url_steamcommunity_timeout_s); var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); #if DEBUG