Skip to content

Commit 799dd62

Browse files
committed
feat: 优化代码
1 parent 71cd085 commit 799dd62

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

src/OSharp.Utils/Http/HttpExtensions.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// -----------------------------------------------------------------------
1+
// -----------------------------------------------------------------------
22
// <copyright file="HttpClientExtensions.cs" company="OSharp开源团队">
33
// Copyright (c) 2014-2017 OSharp. All rights reserved.
44
// </copyright>
@@ -13,10 +13,9 @@
1313
using System.Net.Http;
1414
using System.Net.Http.Headers;
1515
using System.Text;
16+
using System.Text.Json;
1617
using System.Threading.Tasks;
1718

18-
using Newtonsoft.Json;
19-
2019
using OSharp.Extensions;
2120

2221

@@ -38,7 +37,7 @@ public static async Task<TResult> GetAsync<TResult>(this HttpClient client, stri
3837
{
3938
return json as TResult;
4039
}
41-
return JsonConvert.DeserializeObject<TResult>(json);
40+
return JsonSerializer.Deserialize<TResult>(json);
4241
}
4342

4443
/// <summary>
@@ -66,14 +65,15 @@ public static async Task<TResult> PostAsync<TResult>(this HttpClient client, str
6665
HttpResponseMessage response = await client.PostAsync(url, data);
6766
if (!response.IsSuccessStatusCode)
6867
{
69-
return default(TResult);
68+
return null;
7069
}
7170
string json = await response.Content.ReadAsStringAsync();
7271
if (typeof(TResult) == typeof(string))
7372
{
7473
return json as TResult;
7574
}
76-
return JsonConvert.DeserializeObject<TResult>(json);
75+
76+
return JsonSerializer.Deserialize<TResult>(json);
7777
}
7878

7979
/// <summary>
@@ -213,4 +213,4 @@ public static string GetHostUrl(this Uri uri)
213213
return $"{uri.Scheme}://{uri.Host}/";
214214
}
215215
}
216-
}
216+
}

src/OSharp.Utils/Http/JsonContent.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// -----------------------------------------------------------------------
1+
// -----------------------------------------------------------------------
22
// <copyright file="JsonContent.cs" company="OSharp开源团队">
33
// Copyright (c) 2014-2019 OSharp. All rights reserved.
44
// </copyright>
@@ -9,8 +9,7 @@
99

1010
using System.Net.Http;
1111
using System.Text;
12-
13-
using Newtonsoft.Json;
12+
using System.Text.Json;
1413

1514

1615
namespace OSharp.Http
@@ -24,7 +23,7 @@ public class JsonContent : StringContent
2423
/// 初始化一个<see cref="JsonContent"/>类型的新实例
2524
/// </summary>
2625
public JsonContent(object obj)
27-
: base(JsonConvert.SerializeObject(obj), Encoding.UTF8, "application/json")
26+
: base(JsonSerializer.Serialize(obj), Encoding.UTF8, "application/json")
2827
{ }
2928
}
30-
}
29+
}

src/OSharp.Wpf/Hubs/HubClientBase.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,11 @@ protected virtual async Task OnClosed(Exception error)
161161
{
162162
int delay = Random.Next(0, 5);
163163
await Output.StatusBarCountdown("{0}秒后重试连接通信服务器", delay);
164-
await HubConnection.StartAsync();
165-
Output.StatusBar($"通信服务器连接{(HubConnection.State == HubConnectionState.Connected ? "成功" : "失败")}");
164+
if (HubConnection.State == HubConnectionState.Disconnected)
165+
{
166+
await HubConnection.StartAsync();
167+
Output.StatusBar($"通信服务器连接{(HubConnection.State == HubConnectionState.Connected ? "成功" : "失败")}");
168+
}
166169
}
167170

168171
/// <summary>

0 commit comments

Comments
 (0)