简体   繁体   中英

Why SignalR is not loaded properly when my ASP.NET MVC application is published on IIS using bundle optimization?

It seems that when running my application using SignalR on my development server with IIS I get the error below in the JS console (while it's completely fine running the application locally in Debug):

 Uncaught Error: SignalR: SignalR is not loaded. Please ensure jquery.signalR-x.js is referenced before ~/signalr/js. at hubs:17 at hubs:106 

Line 17 is related to:

if (typeof ($.signalR) !== "function") {
    throw new Error("SignalR: SignalR is not loaded. Please ensure jquery.signalR-x.js is referenced before ~/signalr/js.");
}

Line 106 is just the end of the IIFE.

Here is below BundleConfig class:

public class BundleConfig
{
    public static void RegisterBundles(BundleCollection bundles)
    {
        var scriptBundle = new ScriptBundle("~/Scripts/bundle");

        // jQuery
        scriptBundle.Include("~/Scripts/jquery-3.1.1.min.js");

        // Bootstrap
        scriptBundle.Include("~/Scripts/bootstrap.min.js");

        // Knockout
        scriptBundle.Include("~/Scripts/knockout-{version}.js");

        // SignalR
        scriptBundle.Include("~/Scripts/jquery.signalR-{version}.js");

        // Custom site scripts
        scriptBundle.Include("~/Scripts/scripts.js");
        scriptBundle.Include("~/Scripts/scripts-bootstrap.js");
        scriptBundle.Include("~/Scripts/scripts-devextreme.js");


        var styleBundle = new StyleBundle("~/Content/bundle");

        // Bootstrap
        styleBundle.Include("~/Content/bootstrap-telus-theme.min.css");

        // Font Awesome
        styleBundle.Include("~/Content/font-awesome.min.css");

        // Custom site styles
        styleBundle.Include("~/Content/Site.css");
        styleBundle.Include("~/Content/Site-bootstrap-plus.css");
        styleBundle.Include("~/Content/Site-devextreme-plus.css");


        bundles.Add(scriptBundle);
        bundles.Add(styleBundle);

        #if !DEBUG
        BundleTable.EnableOptimizations = true;
        #endif
    }
}

The doctype on the development server looks like:

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Login</title>
    <link rel="favicon" href="/Content/favicons/favicon.ico" />
    <link rel="favicon" sizes="32x32" href="/Content/favicons/favicon-32.png" />
    <link rel="favicon" sizes="128x128" href="/Content/favicons/favicon-128.png" />
    <link rel="apple-touch-icon" href="/Content/favicons/apple-touch-icon.png" />
    <link rel="apple-touch-icon" sizes="72x72" href="/Content/favicons/apple-touch-icon-72.png" />
    <link rel="apple-touch-icon" sizes="114x114" href="/Content/favicons/apple-touch-icon-114.png" />
    <link rel="apple-touch-icon" sizes="144x144" href="/Content/favicons/apple-touch-icon-144.png" />

    <link href="/Content/bundle?v=KoFrSDNrdsCobjey_0WoDyRHewQDRrB1Ov9eEHg15EI1" rel="stylesheet"/>

    <link href="/Content/DevExtremeBundle?v=dvjWkuzRNPhpuO8epzd1bapDA4VbH3NGrM9XfK6lfH41" rel="stylesheet"/>

    <script src="/Scripts/bundle?v=bk8qAjn5FOZNS6DjmQ8qRN1l5sZzYFEHYS7tNOnf4vk1"></script>

    <script src="/Scripts/DevExtremeBundle?v=EDbcZoJEzBcOEMDXw50S76TV4vRJy8nCjafsccwoAas1"></script>

    <script src="/signalr/hubs" type="text/javascript"></script>
</head>

In local debug (so without the bundle optimization):

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Login</title>
    <link rel="favicon" href="/Content/favicons/favicon.ico" />
    <link rel="favicon" sizes="32x32" href="/Content/favicons/favicon-32.png" />
    <link rel="favicon" sizes="128x128" href="/Content/favicons/favicon-128.png" />
    <link rel="apple-touch-icon" href="/Content/favicons/apple-touch-icon.png" />
    <link rel="apple-touch-icon" sizes="72x72" href="/Content/favicons/apple-touch-icon-72.png" />
    <link rel="apple-touch-icon" sizes="114x114" href="/Content/favicons/apple-touch-icon-114.png" />
    <link rel="apple-touch-icon" sizes="144x144" href="/Content/favicons/apple-touch-icon-144.png" />

    <link href="/Content/bootstrap-telus-theme.min.css" rel="stylesheet"/>
<link href="/Content/font-awesome.min.css" rel="stylesheet"/>
<link href="/Content/Site.css" rel="stylesheet"/>
<link href="/Content/Site-bootstrap-plus.css" rel="stylesheet"/>
<link href="/Content/Site-devextreme-plus.css" rel="stylesheet"/>

    <link href="/Content/dx.common.css" rel="stylesheet"/>
<link href="/Content/dx.telus.light.css" rel="stylesheet"/>

    <script src="/Scripts/jquery-3.1.1.min.js"></script>
<script src="/Scripts/bootstrap.min.js"></script>
<script src="/Scripts/knockout-3.4.2.debug.js"></script>
<script src="/Scripts/jquery.signalR-2.2.2.js"></script>
<script src="/Scripts/scripts.js"></script>
<script src="/Scripts/scripts-bootstrap.js"></script>
<script src="/Scripts/scripts-devextreme.js"></script>

    <script src="/Scripts/cldr.js"></script>
<script src="/Scripts/cldr/event.js"></script>
<script src="/Scripts/cldr/supplemental.js"></script>
<script src="/Scripts/cldr/unresolved.js"></script>
<script src="/Scripts/globalize.js"></script>
<script src="/Scripts/globalize/message.js"></script>
<script src="/Scripts/globalize/number.js"></script>
<script src="/Scripts/globalize/currency.js"></script>
<script src="/Scripts/globalize/date.js"></script>
<script src="/Scripts/jszip.js"></script>
<script src="/Scripts/dx.all.debug.js"></script>
<script src="/Scripts/aspnet/dx.aspnet.data.js"></script>
<script src="/Scripts/aspnet/dx.aspnet.mvc.js"></script>

    <script src="/signalr/hubs" type="text/javascript"></script>
</head>

What is published on the development server does not run with a DEBUG constant, how can I make it work without it? Imho the BundleTable.EnableOptimizations = true; line is not supposed to mess up my signalR bundling and still that's pretty much the only difference I can see between the two. However when running with the development server configuration locally it also works even with the optimization bundle enabled...

The issue happened because bundle optimization is skipping min files. And you bundled JS doesn't have jQuery, which is required for SignalR.

For more details, please check this answer: Bundler not including .min files

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM