Skip to content

Commit

Permalink
Fix Update_VM failing with MessagePack (#227).
Browse files Browse the repository at this point in the history
  • Loading branch information
Dicky Suryadi authored and Dicky Suryadi committed Dec 30, 2019
1 parent ca6ea21 commit 3ff5499
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions DotNetifyLib.SignalR.Owin/DotNetifyHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ limitations under the License.
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using DotNetify.Security;
using System.Linq;

namespace DotNetify
{
Expand Down Expand Up @@ -118,16 +119,7 @@ public override Task OnDisconnected(bool stopCalled)
/// <param name="vmArg">Optional argument that may contain view model's initialization argument and/or request headers.</param>
public void Request_VM(string vmId, object vmArg)
{
JObject data = null;
if (vmArg != null)
{
if (vmArg is JObject)
// Newtonsoft.Json protocol.
data = vmArg as JObject;
else
// MessagePack protocol.
data = JObject.FromObject(vmArg);
}
object data = NormalizeType(vmArg);

try
{
Expand Down Expand Up @@ -159,9 +151,11 @@ public void Request_VM(string vmId, object vmArg)
/// <param name="vmData">View model update data, where key is the property path and value is the property's new value.</param>
public void Update_VM(string vmId, Dictionary<string, object> vmData)
{
var data = vmData?.ToDictionary(x => x.Key, x => NormalizeType(x.Value));

try
{
_hubContext = new DotNetifyHubContext(Context, nameof(Request_VM), vmId, vmData, null, Principal);
_hubContext = new DotNetifyHubContext(Context, nameof(Request_VM), vmId, data, null, Principal);
_hubPipeline.RunMiddlewares(_hubContext, ctx =>
{
Principal = ctx.Principal;
Expand Down Expand Up @@ -265,6 +259,24 @@ internal void HandleMulticastMessage(string messageType, string vmId, string ser

#endregion Server Responses

/// <summary>
/// Normalizes the type of the object argument to JObject when possible.
/// </summary>
/// <param name="data">Arbitrary object.</param>
/// <returns>JObject if the object is convertible; otherwise unchanged.</returns>
private object NormalizeType(object data)
{
if (data == null)
return null;
else if (data is JObject)
// Newtonsoft.Json protocol.
return data as JObject;
else if (!(data.GetType().IsPrimitive || data is string))
// MessagePack protocol.
return JObject.FromObject(data);
return data;
}

/// <summary>
/// Runs the view model filter.
/// </summary>
Expand Down

0 comments on commit 3ff5499

Please sign in to comment.