diff --git a/DotNetifyLib.SignalR.Owin/DotNetifyHub.cs b/DotNetifyLib.SignalR.Owin/DotNetifyHub.cs index d16d2545..5732f5dd 100644 --- a/DotNetifyLib.SignalR.Owin/DotNetifyHub.cs +++ b/DotNetifyLib.SignalR.Owin/DotNetifyHub.cs @@ -23,6 +23,7 @@ limitations under the License. using Newtonsoft.Json; using Newtonsoft.Json.Linq; using DotNetify.Security; +using System.Linq; namespace DotNetify { @@ -118,16 +119,7 @@ public override Task OnDisconnected(bool stopCalled) /// Optional argument that may contain view model's initialization argument and/or request headers. 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 { @@ -159,9 +151,11 @@ public void Request_VM(string vmId, object vmArg) /// View model update data, where key is the property path and value is the property's new value. public void Update_VM(string vmId, Dictionary 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; @@ -265,6 +259,24 @@ internal void HandleMulticastMessage(string messageType, string vmId, string ser #endregion Server Responses + /// + /// Normalizes the type of the object argument to JObject when possible. + /// + /// Arbitrary object. + /// JObject if the object is convertible; otherwise unchanged. + 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; + } + /// /// Runs the view model filter. ///