{"id":307,"date":"2022-08-30T14:59:10","date_gmt":"2022-08-30T14:59:10","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/10\/07\/self-hosted-cross-site-connection-issue-with-signalr-and-mono-and-owin-record-and-share-programming-errors\/"},"modified":"2022-08-30T14:59:10","modified_gmt":"2022-08-30T14:59:10","slug":"self-hosted-cross-site-connection-issue-with-signalr-and-mono-and-owin-record-and-share-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/self-hosted-cross-site-connection-issue-with-signalr-and-mono-and-owin-record-and-share-programming-errors\/","title":{"rendered":"Self hosted cross site connection issue with SignalR and Mono and Owin-Record and share programming errors"},"content":{"rendered":"<p>I&#8217;m about ready to pull my hair out over this problem because all the guides show the same one or two solutions, which seem to be working for everyone but myself.<\/p>\n<p>I&#8217;m using SignalR in a Mono application, using Owin. I&#8217;ve set EnableCrossDomain = true in my HubConfiguration. I&#8217;ve set up a simple test page using javascript to connect. The <code>negotiate<\/code> and <code>ping<\/code> urls are loading without issue and seem to be replying correctly. However I get a cross domain error on the <code>connect<\/code> call.<\/p>\n<p>It&#8217;s definitely using my EnableCrossDomain setting because if I set that to false, <code>negotiate<\/code> doesn&#8217;t work.<\/p>\n<p>Here&#8217;s my Javascript to connect (and yes I&#8217;m including the \/signalr\/hubs script which returns the appropriate javascript):<\/p>\n<pre><code>$.connection.hub.url = 'http:\/\/localhost:7001\/signalr';\n$.connection.hub.logging = true;\n$.connection.hub.start()\n    .done(function () { alert(\"Now connected!\"); })\n    .fail(function () { alert(\"Could not Connect!\"); });\n<\/code><\/pre>\n<p>Also I&#8217;ve tried <code>jQuery.support.cors = true;<\/code> as well but it doesn&#8217;t make any difference.<\/p>\n<p>I also tried connecting to a regular PersistantConnection like so, with the same problem, so it doesn&#8217;t appear to be directly related to hubs:<\/p>\n<pre><code>var connection = $.connection('\/raw');\nconnection.url = 'http:\/\/localhost:7001\/signalr';\nconnection.start()\n    .done(function () { alert(\"Now connected!\"); })\n    .fail(function () { alert(\"Could not Connect!\"); });\n<\/code><\/pre>\n<p>What am I doing wrong here? Why do <code>ping<\/code> and <code>negotiate<\/code> work but not <code>connect<\/code>?<\/p>\n<p>EDIT: Also to make matters weirder, I added some logging and verified that the SignalR <code>CallHandler<\/code> class is adding the <code>Access-Control-Allow-Origin<\/code> header for all requests, including the <code>connect<\/code> request. So it should be working.<\/p>\n<p>EDIT 2: So actually thanks to curl it looks like the server is returning a 500 response code, and since it&#8217;s not also including the header (this must be happening after the code block I added the logging to), Chrome complains about cross site, when really there&#8217;s probably an exception being thrown somewhere in the server. Hopefully I&#8217;ll have this figured out shortly and will post an answer here.<\/p>\n<ol>\n<li>\n<p>So it actually turned out to have nothing to do with cross site access, and everything to do with Mono incompatibilities.<\/p>\n<p>After I enabled exception logging for SignalR (http:\/\/stackoverflow.com\/a\/16577890\/299262) I got the following exception instead of just a blank http 500 error:<\/p>\n<pre><code>System.InvalidOperationException: The connection id is in the incorrect format.\n  at Microsoft.AspNet.SignalR.PersistentConnection.GetConnectionId (Microsoft.AspNet.SignalR.Hosting.HostContext context, System.String connectionToken) [0x00000] in :0 \n  at Microsoft.AspNet.SignalR.PersistentConnection.ProcessRequest (Microsoft.AspNet.SignalR.Hosting.HostContext context) [0x00000] in :0 \n  at Microsoft.AspNet.SignalR.Hubs.HubDispatcher.ProcessRequest (Microsoft.AspNet.SignalR.Hosting.HostContext context) [0x00000] in :0 \n  at Microsoft.AspNet.SignalR.Owin.CallHandler.Invoke (IDictionary`2 environment) [0x00000] in :0 \n  at Microsoft.AspNet.SignalR.Owin.Handlers.HubDispatcherHandler.Invoke (IDictionary`2 environment) [0x00000] in :0 \n  at Microsoft.AspNet.SignalR.Owin.Handlers.PersistentConnectionHandler.Invoke (IDictionary`2 environment) [0x00000] in :0 \n  at Microsoft.Owin.Diagnostics.ShowExceptionsMiddleware.Invoke (IDictionary`2 environment) [0x00000] in \n<\/code><\/pre>\n<p>That led me to find this ticket on the SignalR repo issues list: https:\/\/github.com\/SignalR\/SignalR\/issues\/1914<\/p>\n<p>So in the end, the fix was to comment out the following line in HostDependencyResolverExtensions.cs:<\/p>\n<pre><code>resolver.InitializePerformanceCounters(instanceName, hostShutdownToken);\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-10-07 10:45:07. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m about ready to pull my hair out over this problem because all the guides show the same one or two solutions, which seem to be working for everyone but myself. I&#8217;m using SignalR in a Mono application, using Owin. I&#8217;ve set EnableCrossDomain = true in my HubConfiguration. I&#8217;ve set up a simple test page [&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-307","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/307","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=307"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/307\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}