locked
Asp.Net SignalR vs. Asp.Net Core SignalR RRS feed

  • Question

  • User1122355199 posted

    Hello everyone and thanks for the help in advance.  I am moving a SignalR project from Asp>net to .Net Core 3.1 and need some help and insight.  I have read the articles at https://docs.microsoft.com/en-us/aspnet/core/signalr/version-differences?view=aspnetcore-3.1 and https://docs.microsoft.com/en-us/aspnet/core/signalr/hubcontext?view=aspnetcore-3.1.  I am trying to implement a hub that utilizes Sql Notification:

        public class FaxNotificationHub : Hub
        {
            private SqlDependency dependency;
    
            private readonly IHubContext<FaxNotificationHub> _hubContext;
    
            [HubMethodName("sendFaxNotifications")]
            public List<tbl_Log_TwilioFax> SendFaxNotifications()
            {
    
                List<tbl_Log_Fax> Faxes = new List<tbl_Log_Fax>();
    
                string connectionString = "myString";
                SqlDependency.Start(connectionString);
                SqlConnection connection = new SqlConnection(connectionString);
                connection.Open();
    
                try
                {
                    SqlCommand command = new SqlCommand();
    
                    string commandText = "SELECT [ID] ,[Status],[FaxSid],[ApiVersion],[MediaUrl],[NumPages],[FaxStatus],[From],[To],[BitRate],[AccountSid],[Resolution],[RemoteStationId],[FilePath],[TimeEntered] FROM[myDb].[dbo].[tbl_Log_Fax]";
                    command.CommandText = commandText;
                    command.Connection = connection;
                    command.CommandType = CommandType.Text;
    
                    dependency = new SqlDependency(command);
                    dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
    
                    DataTable dt = new DataTable();
    
                    dependency.AddCommandDependency(command);
    
                    var reader = command.ExecuteReader();
    
                }
                catch (Exception ex)
                {
    
                }
    
                IHubContext context = GlobalHost.ConnectionManager.GetHubContext<FaxNotificationHub>();
                return context.Clients.All.RecieveNotification(Faxes);
    
            }
    
            private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
            {
                
                if (e.Type == SqlNotificationType.Change)
                {
                    FaxNotificationHub nHub = new FaxNotificationHub();
                    nHub.SendFaxNotifications();
                }
            }
        }

    Where I am running into problems is at:

                IHubContext context = GlobalHost.ConnectionManager.GetHubContext<FaxNotificationHub>();
                //return context.Clients.All.RecieveNotification(twilioFaxes);

    Which actually prompts me to Install AspNetCore.SignalR.Core which I don't think is necessary.  I tried initiating a IHubContext, but it doesn't seem to allow me to broadcast to all clients.  Also, since my previous application uses the notification to re-render a partial view, I likely only need a notification that in turn fires an ajax call to the partial view.  Any help would be appreciated.

    Monday, November 2, 2020 9:44 PM

All replies

  • User-939850651 posted

    Hi kmcnet,

    Which actually prompts me to Install AspNetCore.SignalR.Core which I don't think is necessary.  I tried initiating a IHubContext, but it doesn't seem to allow me to broadcast to all clients.  Also, since my previous application uses the notification to re-render a partial view, I likely only need a notification that in turn fires an ajax call to the partial view.  Any help would be appreciated.

    According to your description, it seems that you are using ASP.NET SignaIR in your .Net Core project. Have you read the mentioned official documents carefully?

    First of all, the first point mentioned in the document is that ASP.NET Core SignalR isn't compatible with clients or servers for ASP.NET SignalR.

    Secondly, their support server platforms are obviously different.

    The most important thing is that ASP.NET Core has dependency injection (DI) built into the framework. Services can use DI to access the HubContext. The GlobalHost object that is used in ASP.NET SignalR to get a HubContext doesn't exist in ASP. NET Core SignalR.

    So I think you need to use ASP.NET Core SignaIR.

    Best regards,

    Xudong Peng

    Wednesday, November 4, 2020 5:29 AM
  • User1122355199 posted

    Thanks for the response.  Since you didn't provide any examples, I'm not sure what you are referring to.  The article https://docs.microsoft.com/en-us/aspnet/core/signalr/hubcontext?view=aspnetcore-3.1 comes up under Core 3.1 , but I think you are saying this applies ONLY to SignalR and not SignalR Core.  The article states:

    "Get an instance of IHubContext

    In ASP.NET Core SignalR, you can access an instance of IHubContext via dependency injection. You can inject an instance of IHubContext into a controller, middleware, or other DI service. Use the instance to send messages to clients."

    I've posted the Hub code previously.  IN my Startup.cs, , I have added:

    endpoints.MapHub<FaxNotificationHub>("/sendFaxNotifications");

    My javascript:

    <script src="~/js/signalr/dist/browser/signalr.js"></script>
    <script>
        "use strict";
    
        var connection = new signalR.HubConnectionBuilder().withUrl("/sendFaxNotifications").build();
    
        connection.start().then(function () {
    
        }).catch(function (err) {
            return console.error(err.toString());
        });
    
        connection.on("sendFaxNotifications", function (message) {
    
            alert("New Fax");
        });    
    </script>

    Currently, the hub doesn't seem to be called.  Any help would be appreciated.

    Wednesday, November 4, 2020 1:32 PM
  • User-939850651 posted

    Hi kmcnet,

    I tried to use your code, but there are some problems, for example:

    endpoints.MapHub<FaxNotificationHub>("/sendFaxNotifications");

    The issue you will get:

    So you need to use ASP.NET Core SignaIR, I think you need to make some changes to the code.

    I created a simple example, you could refer to :

    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/lib/signalr/signalr.js"></script>
    <script>
        "use strict";
        var connection = new signalR.HubConnectionBuilder().withUrl("/FaxNotificationHub").build();
    
        connection.on("RecieveNotification", (result) => {
            //console.log(result);
            alert("New Fax");
        });
    
        connection.start().then(function () {
            connection.invoke("SendFaxNotifications");
        }).catch(function (err) {
            return console.error(err.toString());
        });
    
    </script>
    endpoints.MapHub<FaxNotificationHub>("/FaxNotificationHub");
    public async Task SendFaxNotifications()
            {
                List<dynamic> list = new List<dynamic>
                {
                    new{a="a",b="b"},
                    new{a="a1",b="b1"}
                };
                await Clients.All.SendAsync("RecieveNotification", list);
            }

    You could also refer to this document below:

    Tutorial: Get started with ASP.NET Core SignalR

    Best regards,

    Xudong Peng

    Thursday, November 5, 2020 10:13 AM
  • User1122355199 posted

    Thank you for the response.  Here is where I am at.  The javascript:

        <script src="~/lib/jquery/dist/jquery.min.js"></script>
        <script src="~/js/signalr/dist/browser/signalr.js"></script>
        <script>
        "use strict";
        var connection = new signalR.HubConnectionBuilder().withUrl("/FaxNotificationHub").build();
    
        connection.on("RecieveNotification", (result) => {
            //console.log(result);
            alert("New Fax");
        });
    
        connection.start().then(function () {
            connection.invoke("SendFaxNotifications");
        }).catch(function (err) {
            return console.error(err.toString());
        });
    
        </script>

    The mapping in Startup.cs:

    endpoints.MapHub<FaxNotificationHub>("/FaxNotificationHub");

    And the hub itself:

        public class FaxNotificationHub : Hub
        {
    
    
            SendEmail sendEmail = new SendEmail("SendFaxNotifications", "SendFaxNotifications Reached", "");
    
            private SqlDependency dependency;
    
            private readonly IHubContext<FaxNotificationHub> _hubContext;
    
            public override async Task OnConnectedAsync()
            {
                //return base.OnConnectedAsync();
                await base.OnConnectedAsync();
            }
    
            //[HubMethodName("sendFaxNotifications")]
            public async Task SendFaxNotifications()
            {
                SendEmail sendEmail = new SendEmail("SendFaxNotifications", "SendFaxNotifications Reached", "");
    
                string connectionString = "my connection string";
                SqlDependency.Start(connectionString);
                SqlConnection connection = new SqlConnection(connectionString);
                connection.Open();
    
                try
                {
                    SqlCommand command = new SqlCommand();
    
                    string commandText = "SELECT [ID] ,[Status],[FaxSid],[ApiVersion],[MediaUrl],[NumPages],[FaxStatus],[From],[To],[BitRate],[AccountSid],[Resolution],[RemoteStationId],[FilePath],[TimeEntered] FROM[Kidslocal].[dbo].[tbl_Log_TwilioFax]";
                    command.CommandText = commandText;
                    command.Connection = connection;
                    command.CommandType = CommandType.Text;
    
                    dependency = new SqlDependency(command);
                    dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
    
                    DataTable dt = new DataTable();
    
                    dependency.AddCommandDependency(command);
    
                    var reader = command.ExecuteReader();
    
                }
                catch (Exception ex)
                {
    
                }
    
                await Clients.All.SendAsync("RecieveNotification", "Changed");
            }

    IN the console, I am getting the message:

    [2020-11-05T22:54:47.755Z] Error: Connection disconnected with error 'Error: Server returned an error on close: Connection closed with an error.'. signalr.js:3082:40
     

    And

    Uncaught (in promise) Error: Server returned an error on close: Connection closed with an error.
        processIncomingData https://localhost:44308/js/signalr/dist/browser/signalr.js:2379
        onreceive https://localhost:44308/js/signalr/dist/browser/signalr.js:1923
        onmessage https://localhost:44308/js/signalr/dist/browser/signalr.js:4728
        connect https://localhost:44308/js/signalr/dist/browser/signalr.js:4725
        connect https://localhost:44308/js/signalr/dist/browser/signalr.js:4687
        step https://localhost:44308/js/signalr/dist/browser/signalr.js:4649
        verb https://localhost:44308/js/signalr/dist/browser/signalr.js:4630
        __awaiter https://localhost:44308/js/signalr/dist/browser/signalr.js:4624
        __awaiter https://localhost:44308/js/signalr/dist/browser/signalr.js:4620
        connect https://localhost:44308/js/signalr/dist/browser/signalr.js:4669
        startTransport https://localhost:44308/js/signalr/dist/browser/signalr.js:3842
        createTransport https://localhost:44308/js/signalr/dist/browser/signalr.js:3792
        step https://localhost:44308/js/signalr/dist/browser/signalr.js:3411
        verb https://localhost:44308/js/signalr/dist/browser/signalr.js:3392
        __awaiter https://localhost:44308/js/signalr/dist/browser/signalr.js:3386
        __awaiter https://localhost:44308/js/signalr/dist/browser/signalr.js:3382
        createTransport https://localhost:44308/js/signalr/dist/browser/signalr.js:3745
        startInternal https://localhost:44308/js/signalr/dist/browser/signalr.js:3665
        step https://localhost:44308/js/signalr/dist/browser/signalr.js:3411
        verb https://localhost:44308/js/signalr/dist/browser/signalr.js:3392
        fulfilled https://localhost:44308/js/signalr/dist/browser/signalr.js:3383
        promise callback*step https://localhost:44308/js/signalr/dist/browser/signalr.js:3385
        __awaiter https://localhost:44308/js/signalr/dist/browser/signalr.js:3386
        __awaiter https://localhost:44308/js/signalr/dist/browser/signalr.js:3382
        startInternal https://localhost:44308/js/signalr/dist/browser/signalr.js:3596
        start https://localhost:44308/js/signalr/dist/browser/signalr.js:3480
        step https://localhost:44308/js/signalr/dist/browser/signalr.js:3411
        verb https://localhost:44308/js/signalr/dist/browser/signalr.js:3392
        __awaiter https://localhost:44308/js/signalr/dist/browser/signalr.js:3386
        __awaiter https://localhost:44308/js/signalr/dist/browser/signalr.js:3382
        start https://localhost:44308/js/signalr/dist/browser/signalr.js:3468
        startInternal https://localhost:44308/js/signalr/dist/browser/signalr.js:2036
        step https://localhost:44308/js/signalr/dist/browser/signalr.js:1883
        verb https://localhost:44308/js/signalr/dist/browser/signalr.js:1864
        __awaiter https://localhost:44308/js/signalr/dist/browser/signalr.js:1858
        __awaiter https://localhost:44308/js/signalr/dist/browser/signalr.js:1854
        startInternal https://localhost:44308/js/signalr/dist/browser/signalr.js:2024
        startWithStateTransitions https://localhost:44308/js/signalr/dist/browser/signalr.js:2006
        step https://localhost:44308/js/signalr/dist/browser/signalr.js:1883
        verb https://localhost:44308/js/signalr/dist/browser/signalr.js:1864
        __awaiter https://localhost:44308/js/signalr/dist/browser/signalr.js:1858
        __awaiter https://localhost:44308/js/signalr/dist/browser/signalr.js:1854
        startWithStateTransitions https://localhost:44308/js/signalr/dist/browser/signalr.js:1993
        start https://localhost:44308/js/signalr/dist/browser/signalr.js:1989
        <anonymous> https://localhost:44308/Fax/Fax:47
    signalr.js:2379:53
        invocationDescriptor.invocationId https://localhost:44308/js/signalr/dist/browser/signalr.js:2252
        cancelCallbacksWithError https://localhost:44308/js/signalr/dist/browser/signalr.js:2634
        forEach self-hosted:206
        cancelCallbacksWithError https://localhost:44308/js/signalr/dist/browser/signalr.js:2632
        connectionClosed https://localhost:44308/js/signalr/dist/browser/signalr.js:2497
        onclose https://localhost:44308/js/signalr/dist/browser/signalr.js:1924
        stopConnection https://localhost:44308/js/signalr/dist/browser/signalr.js:3921
        onclose https://localhost:44308/js/signalr/dist/browser/signalr.js:3841
        close https://localhost:44308/js/signalr/dist/browser/signalr.js:4786
        stop https://localhost:44308/js/signalr/dist/browser/signalr.js:4766
        stopInternal https://localhost:44308/js/signalr/dist/browser/signalr.js:3574
        step https://localhost:44308/js/signalr/dist/browser/signalr.js:3411
        verb https://localhost:44308/js/signalr/dist/browser/signalr.js:3392
        fulfilled https://localhost:44308/js/signalr/dist/browser/signalr.js:3383
        (Async: promise callback)
        step https://localhost:44308/js/signalr/dist/browser/signalr.js:3385
        __awaiter https://localhost:44308/js/signalr/dist/browser/signalr.js:3386
        __awaiter https://localhost:44308/js/signalr/dist/browser/signalr.js:3382
        stopInternal https://localhost:44308/js/signalr/dist/browser/signalr.js:3550
        stop https://localhost:44308/js/signalr/dist/browser/signalr.js:3537
        step https://localhost:44308/js/signalr/dist/browser/signalr.js:3411
        verb https://localhost:44308/js/signalr/dist/browser/signalr.js:3392
        __awaiter https://localhost:44308/js/signalr/dist/browser/signalr.js:3386
        __awaiter https://localhost:44308/js/signalr/dist/browser/signalr.js:3382
        stop https://localhost:44308/js/signalr/dist/browser/signalr.js:3518
        stopInternal https://localhost:44308/js/signalr/dist/browser/signalr.js:2145
        processIncomingData https://localhost:44308/js/signalr/dist/browser/signalr.js:2388
        onreceive https://localhost:44308/js/signalr/dist/browser/signalr.js:1923
        onmessage https://localhost:44308/js/signalr/dist/browser/signalr.js:4728
        (Async: EventHandlerNonNull)
        connect https://localhost:44308/js/signalr/dist/browser/signalr.js:4725
        connect https://localhost:44308/js/signalr/dist/browser/signalr.js:4687
        step https://localhost:44308/js/signalr/dist/browser/signalr.js:4649
        verb https://localhost:44308/js/signalr/dist/browser/signalr.js:4630
        __awaiter https://localhost:44308/js/signalr/dist/browser/signalr.js:4624
        __awaiter https://localhost:44308/js/signalr/dist/browser/signalr.js:4620
        connect https://localhost:44308/js/signalr/dist/browser/signalr.js:4669
        startTransport https://localhost:44308/js/signalr/dist/browser/signalr.js:3842
        createTransport https://localhost:44308/js/signalr/dist/browser/signalr.js:3792
        step https://localhost:44308/js/signalr/dist/browser/signalr.js:3411
        verb https://localhost:44308/js/signalr/dist/browser/signalr.js:3392
        __awaiter https://localhost:44308/js/signalr/dist/browser/signalr.js:3386
        __awaiter https://localhost:44308/js/signalr/dist/browser/signalr.js:3382
        createTransport https://localhost:44308/js/signalr/dist/browser/signalr.js:3745
        startInternal https://localhost:44308/js/signalr/dist/browser/signalr.js:3665
        step https://localhost:44308/js/signalr/dist/browser/signalr.js:3411
        verb https://localhost:44308/js/signalr/dist/browser/signalr.js:3392
        fulfilled https://localhost:44308/js/signalr/dist/browser/signalr.js:3383
        (Async: promise callback)
        step https://localhost:44308/js/signalr/dist/browser/signalr.js:3385
        __awaiter https://localhost:44308/js/signalr/dist/browser/signalr.js:3386
        __awaiter https://localhost:44308/js/signalr/dist/browser/signalr.js:3382
        startInternal https://localhost:44308/js/signalr/dist/browser/signalr.js:3596
        start https://localhost:44308/js/signalr/dist/browser/signalr.js:3480
        step https://localhost:44308/js/signalr/dist/browser/signalr.js:3411
        verb https://localhost:44308/js/signalr/dist/browser/signalr.js:3392
        __awaiter https://localhost:44308/js/signalr/dist/browser/signalr.js:3386
        __awaiter https://localhost:44308/js/signalr/dist/browser/signalr.js:3382
        start https://localhost:44308/js/signalr/dist/browser/signalr.js:3468
        startInternal https://localhost:44308/js/signalr/dist/browser/signalr.js:2036
        step https://localhost:44308/js/signalr/dist/browser/signalr.js:1883
        verb https://localhost:44308/js/signalr/dist/browser/signalr.js:1864
        __awaiter https://localhost:44308/js/signalr/dist/browser/signalr.js:1858
        __awaiter https://localhost:44308/js/signalr/dist/browser/signalr.js:1854
        startInternal https://localhost:44308/js/signalr/dist/browser/signalr.js:2024
        startWithStateTransitions https://localhost:44308/js/signalr/dist/browser/signalr.js:2006
        step https://localhost:44308/js/signalr/dist/browser/signalr.js:1883
        verb https://localhost:44308/js/signalr/dist/browser/signalr.js:1864
        __awaiter https://localhost:44308/js/signalr/dist/browser/signalr.js:1858
        __awaiter https://localhost:44308/js/signalr/dist/browser/signalr.js:1854
        startWithStateTransitions https://localhost:44308/js/signalr/dist/browser/signalr.js:1993
        start https://localhost:44308/js/signalr/dist/browser/signalr.js:1989
        <anonymous> https://localhost:44308/Fax/Fax:47
    

    Not sure where to go with this.

    Thursday, November 5, 2020 10:59 PM
  • User-939850651 posted

    Hi kmcnet,

    I tested it with your code, I didn't get the same issue, I made some necessary modifications, and the corresponding settings:

    public class FaxNotificationHub : Hub
        {
            //SendEmail sendEmail = new SendEmail("SendFaxNotifications", "SendFaxNotifications Reached", "");
    
            private SqlDependency dependency;
    
            public override async Task OnConnectedAsync()
            {
                //return base.OnConnectedAsync();
                await base.OnConnectedAsync();
            }
    
            //[HubMethodName("sendFaxNotifications")]
            public async Task SendFaxNotifications()
            {
                //SendEmail sendEmail = new SendEmail("SendFaxNotifications", "SendFaxNotifications Reached", "");
    
                string connectionString = "data source=.; database=TestDB; integrated security=SSPI";
                SqlDependency.Start(connectionString);
                SqlConnection connection = new SqlConnection(connectionString);
                connection.Open();
    
                try
                {
                    SqlCommand command = new SqlCommand();
    
                    string commandText = "select * from tbl_default";
                    command.CommandText = commandText;
                    command.Connection = connection;
                    command.CommandType = CommandType.Text;
    
                    dependency = new SqlDependency(command);
                    dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
    
                    DataTable dt = new DataTable();
    
                    dependency.AddCommandDependency(command);
    
                    var reader = command.ExecuteReader();
    
                }
                catch (Exception ex)
                {
                }
    
               await Clients.All.SendAsync("RecieveNotification", "Changed");
            }
    
            private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
            {
                if (e.Type == SqlNotificationType.Change)
                {
                    FaxNotificationHub nHub = new FaxNotificationHub();
                    nHub.SendFaxNotifications();
                }
            }
        }
    in Startup.cs
    
    public void ConfigureServices(IServiceCollection services)
            {
                services.AddControllersWithViews();
                services.AddSignalR();
            }
    
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseExceptionHandler("/Home/Error");
                    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                    app.UseHsts();
                }
                app.UseHttpsRedirection();
                app.UseStaticFiles();
                app.UseRouting();
                app.UseAuthorization();
    
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapHub<FaxNotificationHub>("/FaxNotificationHub");
                    endpoints.MapControllerRoute(
                        name: "default",
                        pattern: "{controller=Home}/{action=Index}/{id?}");
                });
            }
    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/lib/signalr/signalr.js"></script>
    <script>
        "use strict";
    
        var connection = new signalR.HubConnectionBuilder().withUrl("/FaxNotificationHub").build();
    
        connection.on("RecieveNotification", (result) => {
            console.log(result);
            alert("New Fax");
        });
    
        connection.start().then(function () {
            connection.invoke("SendFaxNotifications").catch(err => console.error(err));
        }).catch(function (err) {
            return console.error(err.toString());
        });
    </script>
    ALTER DATABASE TestDB SET ENABLE_BROKER 

    Result:

    So I am wondering if you have any differences or modifications elsewhere.

    Best regards,

    Xudong Peng

    Friday, November 6, 2020 7:35 AM
  • User1122355199 posted

    Thanks again for the help.  There is one difference in that the path to my SignalR.js that is:

    <script src="~/js/signalr/dist/browser/signalr.js"></script>

    This was the default installation for the SignalR javascript install.  The script appears to resolve correctly in the console.  Here is the full code for the rest of the application.  The javascript in the page:

    <html>
    <head>
        <script src="~/lib/jquery/dist/jquery.min.js"></script>
        <script src="~/js/signalr/dist/browser/signalr.js"></script>
        <script>
            "use strict";
    
            var connection = new signalR.HubConnectionBuilder().withUrl("/FaxNotificationHub").build();
    
            connection.on("RecieveNotification", (result) => {
                console.log(result);
                alert("New Fax");
            });
    
            connection.start().then(function () {
                connection.invoke("SendFaxNotifications").catch(err => console.error(err));
            }).catch(function (err) {
                return console.error(err.toString());
            });
        </script>
    </head>
    <body>   
    </body>
    </html>

    Startup.cs:

        public class Startup
        {
    
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public IConfiguration Configuration { get; }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddRazorPages(options =>
                {
                    options.Conventions.AuthorizePage("/Login");
                });
    
                services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(config => {
                    config.LoginPath = "/Home/Login";
                    //config.ExpireTimeSpan = TimeSpan.FromMinutes(45);
    
                });          
    
                services.AddControllersWithViews();
    
                services.AddSignalR();
                
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseExceptionHandler("/Home/Error");
                    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                    app.UseHsts();
                }
                app.UseHttpsRedirection();
                app.UseStaticFiles();
    
                app.UseRouting();
    
                app.UseStaticFiles();
    
                app.UseAuthentication();
                app.UseAuthorization();
    
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllerRoute(
                        name: "default",
                        pattern: "{controller=Home}/{action=Index}/{id?}");
    
                    endpoints.MapHub<FaxNotificationHub>("/FaxNotificationHub");
    
                });
            }
        }

    And the Hub:

        public class FaxNotificationHub : Hub
        {
            //
            //  https://docs.microsoft.com/en-us/aspnet/core/signalr/hubs?view=aspnetcore-3.1
            //  https://docs.microsoft.com/en-us/aspnet/core/signalr/javascript-client?view=aspnetcore-3.1
            //  https://docs.microsoft.com/en-us/aspnet/core/signalr/hubcontext?view=aspnetcore-3.1
            //  https://docs.microsoft.com/en-us/aspnet/core/signalr/javascript-client?view=aspnetcore-3.1
            //
    
            SendEmail sendEmail = new SendEmail("SendFaxNotifications", "SendFaxNotifications Reached", "");
    
            private SqlDependency dependency;
    
            private readonly IHubContext<FaxNotificationHub> _hubContext;
    
            public override async Task OnConnectedAsync()
            {
                //return base.OnConnectedAsync();
                await base.OnConnectedAsync();
            }
    
            //[HubMethodName("sendFaxNotifications")]
            public async Task SendFaxNotifications()
            {
                SendEmail sendEmail = new SendEmail("SendFaxNotifications", "SendFaxNotifications Reached", "");
    
                string connectionString = "myConnectionDb";
                SqlDependency.Start(connectionString);
                SqlConnection connection = new SqlConnection(connectionString);
                connection.Open();
    
                try
                {
                    SqlCommand command = new SqlCommand();
    
                    string commandText = "SELECT [ID] ,[Status],[FaxSid],[ApiVersion],[MediaUrl],[NumPages],[FaxStatus],[From],[To],[BitRate],[AccountSid],[Resolution],[RemoteStationId],[FilePath],[TimeEntered] FROM[Kidslocal].[dbo].[tbl_Log_TwilioFax]";
                    command.CommandText = commandText;
                    command.Connection = connection;
                    command.CommandType = CommandType.Text;
    
                    dependency = new SqlDependency(command);
                    dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
    
                    DataTable dt = new DataTable();
    
                    dependency.AddCommandDependency(command);
    
                    var reader = command.ExecuteReader();
    
                }
                catch (Exception ex)
                {
    
                }
    
                //await _hubContext.Clients.All.SendAsync("ReceiveMessage", $"New Fax Received: {DateTime.Now}");
                //await _hubContext.Clients.All.SendAsync($"New Fax Received: {DateTime.Now}");
                await Clients.All.SendAsync("RecieveNotification", "Changed");
            }
    
            private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
            {        
    
    
                if (e.Type == SqlNotificationType.Change)
                {
                    FaxNotificationHub nHub = new FaxNotificationHub();
                    nHub.SendFaxNotifications();
                }
            }
        }

    The project is a MVC Core 3.1 developed with Visual Studio 2019.  I have not installed any SignalR components other than the javascript package.  I am receiving the error on both my development machine and production machine.  Not sure what to look at next.

    Saturday, November 7, 2020 5:45 PM
  • User1122355199 posted

    As a followup to my previous post, I followed the article at https://docs.microsoft.com/en-us/aspnet/core/tutorials/signalr?view=aspnetcore-3.1&tabs=visual-studio just to see if my project would supports a basic Asp.Net Core SignalR and it does work, so I believe it rules out any missing dependencies install errors.  So I am assuming the problem is coming from my code itself.

    Sunday, November 8, 2020 6:49 PM