Geeks With Blogs

News

Microsoft MVP


Moderator at CodeASP.NET


Quiz Master




free counters
Free counters
Added on January 19,2012


Follow Me @vmsdurano

A bit About Me



Disclaimer
The opinions expressed herein are my own personal opinions and does not represent the opinions of my employers. Nor does it represent the opinion of my dog, because I don’t have one.


Vinz' Blog (ProudMonkey) "Code, Beer and Music ~ my way of being a programmer"

ASP.NET SignalR is pretty new and very exciting feature in ASP.NET. You might want to play around with it and may encounter some hiccups during your implementation. This post  will cover some issues that you may encounter and how to avoid it.

If you have declared the following scripts in your View(.cshtml/.aspx)

<script src="~/Scripts/jquery.signalR-1.0.1.min.js"></script>
<script src="/signalr/hubs" type="text/javascript"></script>
<script type="text/javascript">
     $(function () {
         var chat = $.connection.yourHub;
     });
</script>

And run the page you will end up getting the following error below:

Exception 1: 0x800a139e - JavaScript runtime error: SignalR: jQuery not found. Please ensure jQuery is referenced before the SignalR.js file.

Why? Well the error above is pretty logical. That simply means that the jQuery script should be added before the SignalR script.

Now most likely you will then go ahead and add a reference to jQuery at the very top before you reference any scripts. So your updated script reference order will now be:

<script src="~/Scripts/jquery-1.8.2.min.js"></script> 
<script src="~/Scripts/jquery.signalR-1.0.1.min.js"></script> 
<script src="/signalr/hubs" type="text/javascript"></script> 
<script type="text/javascript"> 
  $(function () { 
var chat = $.connection.yourHub; 
}); 
</script>

Running the page again will give you the exception below:

Exception 2: JavaScript runtime error: Unable to get property 'yourHub' of undefined or null reference JavaScript runtime error: Unable to get property 'yourHub' of undefined or null reference

Heck, Why is that? Here's the thing.. If you look at the _Layout.cshtml file you will notice that the default template in MVC 4.5 added the bundling below for jQuery scripts at the bottom before the closing tag of the <body> element.

@Scripts.Render("~/bundles/jquery") 
@RenderSection("scripts", required: false) 

So in this case when the page loads up it will give you this script reference order:

  1. jQuery script
  2. SignalR script
  3. /signalr/hubs
  4. your custom defined script
  5. jQuery script

If you noticed the reference to jQuery gets duplicated which causes the exception above.

To avoid that exception you must remove the reference to your jQuery in your View(.cshtml/.aspx) because it's already bundled it for you for page performance/optimization. And instead you can put all your remaining scripts within the scripts section defined in @RenderSection. So the code will be like this:

@section scripts{ 
     @Scripts.Render("~/Scripts/jquery.signalR-1.0.1.min.js") 
     @Scripts.Render("/signalr/hubs") 
     <script type="text/javascript"> 
         $(function () { 
             var chat = $.connection.yourHub; 
         }); 
    </script> 
} 

And finally, make sure that you have defined this line below in your Global.asax.cs within Application_Start() event to avoid exception 3:

RouteTable.Routes.MapHubs();

Note: make sure that MapHubs() is called before RegisterRoutes() in your Application_Start event.

Exception 3: 0x800a138f - JavaScript runtime error: Unable to get property 'client' of undefined or null reference0x800a138f - JavaScript runtime error: Unable to get property 'client' of undefined or null reference

That's it! I hope someone find this post useful.

Technorati Tags: ,,

 

 

Posted on Tuesday, March 5, 2013 4:24 PM ASP.NET , MVC , SignalR | Back to top


Comments on this post: ASP.NET 4.5 - FAQs on SignalR Script Exceptions

comments powered by Disqus

Copyright © Vincent Maverick Durano | Powered by: GeeksWithBlogs.net | Join free