-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix request\response deserialization Multiple SignalR hub support Migrate from Newtonsoft.Json to System.Text.Json JSON WART event beautifier
- Loading branch information
1 parent
76565e4
commit e1330af
Showing
20 changed files
with
498 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
// (c) 2019 Francesco Del Re <[email protected]> | ||
// This code is licensed under MIT license (see LICENSE.txt for details) | ||
using System; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace WART_Client | ||
{ | ||
|
@@ -9,19 +10,28 @@ public class Program | |
private static void Main() | ||
{ | ||
Console.WriteLine("Starting WartTestClient"); | ||
|
||
#region JWT Client Test | ||
|
||
WartTestClientJwt.ConnectAsync(); | ||
var configuration = new ConfigurationBuilder() | ||
.AddJsonFile("appsettings.json") | ||
.Build(); | ||
|
||
#endregion | ||
var wartHubUrl = $"{configuration["Scheme"]}://{configuration["Host"]}:{configuration["Port"]}/{configuration["Hubname"]}"; | ||
|
||
#region No authentication Client Test | ||
Console.WriteLine($"Connecting to {wartHubUrl}"); | ||
|
||
//WartTestClient.ConnectAsync(); | ||
var auth = configuration["AuthenticationJwt"]; | ||
|
||
#endregion | ||
if (bool.Parse(auth)) | ||
{ | ||
var key = configuration["Key"]; | ||
WartTestClientJwt.ConnectAsync(wartHubUrl, key); | ||
} | ||
else | ||
{ | ||
WartTestClient.ConnectAsync(wartHubUrl); | ||
} | ||
|
||
Console.WriteLine($"Connected to {wartHubUrl}"); | ||
Console.ReadLine(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Scheme": "https", | ||
"Host": "localhost", | ||
"Port": "50980", | ||
"Hubname": "warthub", | ||
"AuthenticationJwt": "true", | ||
"Key": "dn3341fmcscscwe28419brhwbwgbss4t" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
// (c) 2019 Francesco Del Re <[email protected]> | ||
// This code is licensed under MIT license (see LICENSE.txt for details) | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using WART_Core.Serialization; | ||
|
||
namespace WART_Core.Entity | ||
{ | ||
|
@@ -14,7 +16,9 @@ public class WartEvent | |
public string HttpMethod { get; set; } | ||
public string HttpPath { get; set; } | ||
public string RemoteAddress { get; set; } | ||
[JsonConverter(typeof(JsonArrayOrObjectStringConverter))] | ||
public string JsonRequestPayload { get; set; } | ||
[JsonConverter(typeof(JsonArrayOrObjectStringConverter))] | ||
public string JsonResponsePayload { get; set; } | ||
public string ExtraInfo { get; set; } | ||
|
||
|
@@ -49,14 +53,20 @@ public WartEvent(string httpMethod, string httpPath, string remoteAddress) | |
/// <param name="remoteAddress"></param> | ||
public WartEvent(object request, object response, string httpMethod, string httpPath, string remoteAddress) | ||
{ | ||
var serializeOptions = new JsonSerializerOptions | ||
{ | ||
WriteIndented = true, | ||
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping | ||
}; | ||
|
||
this.EventId = Guid.NewGuid(); | ||
this.TimeStamp = DateTime.Now; | ||
this.UtcTimeStamp = DateTime.UtcNow; | ||
this.HttpMethod = httpMethod; | ||
this.HttpPath = httpPath; | ||
this.RemoteAddress = remoteAddress; | ||
this.JsonRequestPayload = request != null ? JsonConvert.SerializeObject(request) : string.Empty; | ||
this.JsonResponsePayload = response != null ? JsonConvert.SerializeObject(response) : string.Empty; | ||
this.JsonRequestPayload = request != null ? JsonSerializer.Serialize(request, serializeOptions) : string.Empty; | ||
this.JsonResponsePayload = response != null ? JsonSerializer.Serialize(response, serializeOptions) : string.Empty; | ||
} | ||
|
||
/// <summary> | ||
|
@@ -65,7 +75,13 @@ public WartEvent(object request, object response, string httpMethod, string http | |
/// <returns></returns> | ||
public override string ToString() | ||
{ | ||
return JsonConvert.SerializeObject(this); | ||
var serializeOptions = new JsonSerializerOptions | ||
{ | ||
WriteIndented = true, | ||
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping | ||
}; | ||
|
||
return JsonSerializer.Serialize(this, serializeOptions); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -75,8 +91,8 @@ public override string ToString() | |
/// <returns></returns> | ||
public T GetRequestObject<T>() where T : class | ||
{ | ||
return string.IsNullOrEmpty(JsonRequestPayload) ? | ||
JsonConvert.DeserializeObject<T>(JsonRequestPayload) : null; | ||
return !string.IsNullOrEmpty(JsonRequestPayload) ? | ||
JsonSerializer.Deserialize<T>(JsonRequestPayload) : null; | ||
} | ||
|
||
/// <summary> | ||
|
@@ -86,8 +102,8 @@ public T GetRequestObject<T>() where T : class | |
/// <returns></returns> | ||
public T GetResponseObject<T>() where T : class | ||
{ | ||
return string.IsNullOrEmpty(JsonResponsePayload) ? | ||
JsonConvert.DeserializeObject<T>(JsonResponsePayload) : null; | ||
return !string.IsNullOrEmpty(JsonResponsePayload) ? | ||
JsonSerializer.Deserialize<T>(JsonResponsePayload) : null; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.