Search code examples
asp.net-coresignalrsignalr.clientasp.net-core-signalr

signalR is undefined - unable to resolve signalR in aspnet core web app


I fail to understand why i can't seem to resolve SignalR client library in my aspnet core app. I have included the library, referenced in index.html however when javascript library do not see the reference.

Created a default react project using VS2019 and .net core 3.0 https://github.com/rohitisinhk/signalRIssue

{
  "version": "1.0",
  "defaultProvider": "cdnjs",
  "libraries": [
    {
      "provider": "unpkg",
      "library": "@aspnet/[email protected]",
      "destination": "lib/@aspnet/signalr/",
      "files": [
        "dist/browser/signalr.js",
        "dist/browser/signalr.js.map",
        "dist/browser/signalr.min.js",
        "dist/browser/signalr.min.js.map"
      ]
    }
  ]
}

In the html file i referenced the signalr.js https://github.com/rohitisinhk/signalRIssue/blob/master/SignalRWebApp/ClientApp/public/index.html

<script src="/lib/@aspnet/signalr/dist/browser/signalr.js"></script>

However in the javascript file i can't seem to resolve signalR

https://github.com/rohitisinhk/signalRIssue/blob/master/SignalRWebApp/ClientApp/src/index.js

const signalRConst = signalR;

Solution

    1. Note you're managing the signalR library with libman, which restores files to lib/@aspnet/signalr/, as a result, the npm has no knowledge about the signalR library.

      If you prefer libman over npm, you could change the signalR to window.signalR

      const signalRConst = window.signalR;
      

      Because you're restoring files to lib/@aspnet/signalr/ instead of wwwroot/lib/@aspnet/signalr/, also don't forget to serve static files from the folder lib/**/*

    2. Or you could use npm to manage the dependencies :

      > cd ClientApp
      > npm install @aspnet/signalr --save
      

      Now you can get the signalR by

      import * as signalR from "@aspnet/signalr";