{"id":2293,"date":"2022-08-30T15:23:39","date_gmt":"2022-08-30T15:23:39","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/05\/error-sending-object-to-net-signalr-client-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:23:39","modified_gmt":"2022-08-30T15:23:39","slug":"error-sending-object-to-net-signalr-client-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/error-sending-object-to-net-signalr-client-collection-of-common-programming-errors\/","title":{"rendered":"Error sending object to .net signalr client-Collection of common programming errors"},"content":{"rendered":"<p>My understanding is that with signalr I can just send objects back and forth. I am trying to set up a .net client to receive notifications that orders have been placed on a web site. I am trying to set up a very simple example so that I understand the concepts. It works great when I am sending a string notification back to the client, but when I try to send an object I get an error:<\/p>\n<pre><code>Microsoft.CSharp.RuntimeBinder.RuntimeBinderException was unhandled by user code\n  HResult=-2146233088\n  Message=The best overloaded method match for 'ConsoleHub.Program.DisplayOrder(ConsoleHub.Order)' has some invalid arguments\n  Source=Anonymously Hosted DynamicMethods Assembly\n  StackTrace:\n       at CallSite.Target(Closure , CallSite , Type , Object )\n       at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid2[T0,T1](CallSite site, T0 arg0, T1 arg1)\n       at ConsoleHub.Program.b__6(Object o) in c:\\Working\\OrderNotifier\\ConsoleHub\\Program.cs:line 23\n       at Microsoft.AspNet.SignalR.Client.Hubs.HubProxyExtensions.c__DisplayClass6`1.b__4(JToken[] args)\n       at Microsoft.AspNet.SignalR.Client.Hubs.Subscription.OnData(JToken[] data)\n       at Microsoft.AspNet.SignalR.Client.Hubs.HubProxy.InvokeEvent(String eventName, JToken[] args)\n       at Microsoft.AspNet.SignalR.Client.Hubs.HubConnection.OnReceived(JToken message)\n       at Microsoft.AspNet.SignalR.Client.Connection.Microsoft.AspNet.SignalR.Client.IConnection.OnReceived(JToken message)\n       at Microsoft.AspNet.SignalR.Client.Transports.HttpBasedTransport.ProcessResponse(IConnection connection, String response, Boolean&amp; timedOut, Boolean&amp; disconnected)\n  InnerException: \n<\/code><\/pre>\n<p>My class:<\/p>\n<pre><code>public class Order\n{\n    public int OrderId { get; set; }\n    public string Name { get; set; }\n    public string OrderItem { get; set; }\n}\n<\/code><\/pre>\n<p>My hub:<\/p>\n<pre><code>using Microsoft.AspNet.SignalR.Hubs;\nusing OrderNotifier.Models;\nusing System.Linq;\nusing System.Threading.Tasks;\n\nnamespace OrderNotifier.Hubs\n{\n\n    public class NotifierHub : Hub\n    {\n        OrderContext db = new OrderContext();\n\n        public void Hello()\n        {\n            Clients.Caller.Welcome(\"hello\");\n        }\n\n    }\n}\n<\/code><\/pre>\n<p>My controller action:<\/p>\n<pre><code>    [HttpPost]\n    public ActionResult Create(Order order)\n    {\n        if (ModelState.IsValid)\n        {\n            db.Orders.Add(order);\n            db.SaveChanges();\n\n            SendNotifier.SendOrderNotification(String.Format(\"{0} ordered {1}\", order.Name, order.OrderItem), order);\n            return RedirectToAction(\"Index\");\n        }\n\n        return View(order);\n    }\n<\/code><\/pre>\n<p>SendNotifier &#8211; which is a little weird because I am having it send both a string version and an object version for testing:<\/p>\n<pre><code>public class SendNotifier\n{\n    public static void SendOrderNotification(string message, Order order)\n    {\n        var context = GlobalHost.ConnectionManager.GetHubContext();\n        context.Clients.All.Notify(message);\n        context.Clients.All.Order(order);\n    }\n}\n<\/code><\/pre>\n<p>And my console application:<\/p>\n<pre><code>using Microsoft.AspNet.SignalR.Client.Hubs;\nusing OrderNotifier.Models;\nusing System;\n\n\nnamespace ConsoleHub\n{\n    class Program\n    {\n        static void Main(string[] args)\n        {\n            var hubConnection = new HubConnection(\"http:\/\/localhost:60692\");\n\n            var order = hubConnection.CreateHubProxy(\"NotifierHub\");\n\n            \/\/\n            \/\/ Set up action handlers\n            \/\/\n            order.On(\"Welcome\", message =&gt; Console.WriteLine(message));\n            order.On(\"Notify\", message =&gt; Console.WriteLine(message));\n            order.On(\"Order\", o =&gt; DisplayOrder(o));\n            hubConnection.Start().Wait();\n\n            order.Invoke(\"Hello\").Wait();\n\n\n            Console.WriteLine(\"Initialized...\");\n            Console.ReadLine();\n        }\n\n\n        public void DisplayOrder(Order o)\n        {\n            Console.WriteLine(String.Format(\"Order object received...\/r\/nOrderId: {0}\/r\/nName: {1}\/r\/nOrderItem: {2}\", o.OrderId, o.Name, o.OrderItem));\n            \/\/Console.WriteLine(o);\n        }\n    }\n}\n<\/code><\/pre>\n<p>If I change the DisplayOrder parameter to be a string it works. I know I could probably manually deserialize it using Json.Net, but my understanding is that I should just be able to work with it as an object and let signalr deserialize. What am I missing?<\/p>\n<ol>\n<li>\n<p>You&#8217;re using the dynamic object overload of On. You need to specify the type:<\/p>\n<pre><code>order.On(\"Order\", DisplayOrder);\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-01-05 09:54:43. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>My understanding is that with signalr I can just send objects back and forth. I am trying to set up a .net client to receive notifications that orders have been placed on a web site. I am trying to set up a very simple example so that I understand the concepts. It works great when [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2293","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2293"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2293\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}