From 68d768b87194d65e3992cb4a9465fe7a2a53bef5 Mon Sep 17 00:00:00 2001 From: mischa Date: Sun, 5 Nov 2023 13:31:13 +0100 Subject: [PATCH] fix: Edgegap Plugin 'InvalidOperationException: This instance has already started one or more requests.' when trying to Connect->Disconnect->Reconnect --- .../Hosting/Edgegap/Editor/EdgegapWindow.cs | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/Assets/Mirror/Hosting/Edgegap/Editor/EdgegapWindow.cs b/Assets/Mirror/Hosting/Edgegap/Editor/EdgegapWindow.cs index 2cee55100..def5564e4 100755 --- a/Assets/Mirror/Hosting/Edgegap/Editor/EdgegapWindow.cs +++ b/Assets/Mirror/Hosting/Edgegap/Editor/EdgegapWindow.cs @@ -250,16 +250,29 @@ string selectedApiTokenValue { SetToolUIState(ToolState.Connecting); - _httpClient.BaseAddress = new Uri(selectedApiEnvironment.GetApiUrl()); + // MIRROR CHANGE /////////////////////////////////////////////////// + // the old code throws an exception when trying to connect again after disconnecting: + // "InvalidOperationException: This instance has already started one or more requests. Properties can only be modified before sending the first request." + // _httpClient.BaseAddress = new Uri(selectedApiEnvironment.GetApiUrl()); + // string path = $"/v1/app/{selectedAppName}/version/{selectedAppVersionName}"; + // _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + // _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("token", selectedApiTokenValue); + // HttpResponseMessage response = await _httpClient.GetAsync(path); + // + // solution: https://stackoverflow.com/questions/51478525/httpclient-this-instance-has-already-started-one-or-more-requests-properties-ca + // "Rather than setting DefaultRequestHeaders, set the headers on each HttpRequestMessage you are sending." + + // Create the request message + string baseUrl = selectedApiEnvironment.GetApiUrl(); string path = $"/v1/app/{selectedAppName}/version/{selectedAppVersionName}"; - - // Headers - _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); - _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("token", selectedApiTokenValue); - - // Make HTTP request - HttpResponseMessage response = await _httpClient.GetAsync(path); + Uri fullUrl = new Uri(new Uri(baseUrl), path); + HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, fullUrl); + request.Headers.Accept.Clear(); + request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + request.Headers.Authorization = new AuthenticationHeaderValue("token", selectedApiTokenValue); + HttpResponseMessage response = await _httpClient.SendAsync(request); + // END MIRROR CHANGE /////////////////////////////////////////////// if (response.IsSuccessStatusCode) {