Using signalR with nopcommerce 4.2

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
4 года назад
Hello Dears
I am trying to use signalR with nopcommerce 4.2 but it is not working with me .

I have created Hub class and html and javascript needed code
I have added signalR to startup.cs file like this.

public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddSignalR();

            return services.ConfigureApplicationServices(_configuration, _hostingEnvironment);
        }

public void Configure(IApplicationBuilder application)
        {
            
            application.ConfigureRequestPipeline();

            application.UseSignalR(routes =>
            {
                routes.MapHub<Chat>("/chat");
            });

        }

I am following this article :
https://codingblast.com/asp-net-core-signalr-simple-chat/
Is that something wrong I do or something I need to do further to make it work ?

It is giving me some client side error when the page running I am showing below :

signalr.js:3088 [2020-02-05T09:13:12.725Z] Information: Normalizing '/chat' to 'http://localhost:15536/chat'.
signalr.js:4709 WebSocket connection to 'ws://localhost:15536/chat?id=lvRemXeWLWEoxKZgOPXzHQ' failed: Error during WebSocket handshake: Unexpected response code: 302
(anonymous) @ signalr.js:4709
signalr.js:3082 [2020-02-05T09:13:12.921Z] Error: Failed to start the transport 'WebSockets': Error: There was an error with the transport.
ConsoleLogger.log @ signalr.js:3082
chat?id=BuCpVHOWkqihl2PQoK63Hg:1 Failed to load resource: the server responded with a status of 404 (Not Found)
signalr.js:3082 [2020-02-05T09:13:13.815Z] Error: Failed to start the transport 'ServerSentEvents': Error: Error occurred
ConsoleLogger.log @ signalr.js:3082
chat?id=TxeMzznEhGV_UuuiWsaIvw&_=1580893993849:1 Failed to load resource: the server responded with a status of 404 (Not Found)
signalr.js:3082 [2020-02-05T09:13:13.947Z] Error: Failed to start the transport 'LongPolling': Error: Not Found
ConsoleLogger.log @ signalr.js:3082
signalr.js:3082 [2020-02-05T09:13:13.948Z] Error: Failed to start the connection: Error: Unable to connect to the server with any of the available transports. WebSockets failed: Error: There was an error with the transport. ServerSentEvents failed: Error: Error occurred LongPolling failed: Error: Not Found
ConsoleLogger.log @ signalr.js:3082
onlinechat:1 Error: Unable to connect to the server with any of the available transports. WebSockets failed: Error: There was an error with the transport. ServerSentEvents failed: Error: Error occurred LongPolling failed: Error: Not Found
(anonymous) @ onlinechat:1

Regards
4 года назад
Here is how I setup signalR for my plugin.

In plugin start up:
public class PluginNopStartup : INopStartup
{
    public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
    {
        services.AddSignalR(hubOptions =>
        {
            hubOptions.KeepAliveInterval = TimeSpan.FromMinutes(1);
        });
    }

    public void Configure(IApplicationBuilder application)
    {
        application.UseSignalR(routes =>
        {
            routes.MapHub<ProductUploadHub>("/uploadproducts");
        });
    }

    public int Order => 11;
}


In hub:
public class ProductUploadHub : Hub
{
    private readonly IWorkContext _workContext;
    private readonly IHubContext<ProductUploadHub> _hubContext;

    public ProductUploadHub(IWorkContext workContext,
        IHubContext<ProductUploadHub> hubContext)
    {
        _hubContext = hubContext;
        _workContext = workContext;
    }

    public void UploadProducts()
    {
        _hubContext.Clients.All.SendAsync("dataSent", mydata);
    }
}


In cshtml:
$(document).ready(function () {
    var connection = new signalR.HubConnectionBuilder()
            .withUrl('/uploadproducts')
            .build();

    connection.on('dataSent', res => {
    });

    function start() {
        connection.start().catch(function (err) {
            setTimeout(function () {
                start();
            }, 10000);
        });
    }

    connection.onclose(function () {
        start();
    });

    start();
});
3 года назад
Hi ,
Please help me .
I am trying to use SingleRChat in nopcommerce but getting client side error .
I have used razor page . Its working in demo application only  but not in nop commerce
======  ================Code In JS=================================
"use strict";

var connection = new signalR.HubConnectionBuilder().withUrl("/chatHub").build();
connection.start();

//Disable send button until connection is established
//document.getElementById("sendButton").disabled = true;

connection.on("ReceiveMessage", function (user, message) {
    var msg = message.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
    var encodedMsg = user + " says " + msg;
    var li = document.createElement("li");
    li.textContent = encodedMsg;
    document.getElementById("messagesList").appendChild(li);
});

connection.start().then(function () {
    document.getElementById("sendButton").disabled = false;
}).catch(function (err) {
    return err.toString(); //console.error(err.toString());
});

document.getElementById("sendButton").addEventListener("click", function (event) {
    connection.start();
    var user = document.getElementById("userInput").value;
    var message = document.getElementById("messageInput").value;
    connection.invoke("SendMessage", user, message).catch(function (err) {
        return err.toString();//; console.error(err.toString());
    });
    event.preventDefault();
});
==================Error in Client Side on browser=============================
WebSocketTransport.ts:64 WebSocket connection to 'ws://localhost:15536/chatHub?id=9Wd8zpB2-rWaCBrSNSBoqg' failed: Error during WebSocket handshake: Unexpected response code: 404
(anonymous) @ WebSocketTransport.ts:64
(anonymous) @ WebSocketTransport.ts:47
step @ WebSocketTransport.ts:2
(anonymous) @ WebSocketTransport.ts:2
(anonymous) @ WebSocketTransport.ts:2
__awaiter @ WebSocketTransport.ts:2
WebSocketTransport.connect @ WebSocketTransport.ts:34
HttpConnection.startTransport @ HttpConnection.ts:414
(anonymous) @ HttpConnection.ts:369
step @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
__awaiter @ HttpConnection.ts:2
HttpConnection.createTransport @ HttpConnection.ts:339
(anonymous) @ HttpConnection.ts:270
step @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
fulfilled @ HttpConnection.ts:2
Utils.ts:168 [2020-10-09T05:51:03.682Z] Error: Failed to start the transport 'WebSockets': Error: There was an error with the transport.
ConsoleLogger.log @ Utils.ts:168
(anonymous) @ HttpConnection.ts:373
step @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
rejected @ HttpConnection.ts:2
Promise.then (async)
step @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
__awaiter @ HttpConnection.ts:2
HttpConnection.createTransport @ HttpConnection.ts:339
(anonymous) @ HttpConnection.ts:270
step @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
fulfilled @ HttpConnection.ts:2
Promise.then (async)
step @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
__awaiter @ HttpConnection.ts:2
HttpConnection.startInternal @ HttpConnection.ts:215
(anonymous) @ HttpConnection.ts:126
step @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
__awaiter @ HttpConnection.ts:2
HttpConnection.start @ HttpConnection.ts:113
(anonymous) @ HubConnection.ts:191
step @ HubConnection.ts:2
(anonymous) @ HubConnection.ts:2
(anonymous) @ HubConnection.ts:2
__awaiter @ HubConnection.ts:2
HubConnection.startInternal @ HubConnection.ts:182
(anonymous) @ HubConnection.ts:170
step @ HubConnection.ts:2
(anonymous) @ HubConnection.ts:2
(anonymous) @ HubConnection.ts:2
__awaiter @ HubConnection.ts:2
HubConnection.startWithStateTransitions @ HubConnection.ts:161
HubConnection.start @ HubConnection.ts:157
(anonymous) @ chat.js:36
:15536/chatHub?id=p92UpYKLBy761rUOPp-QXA:1 GET http://localhost:15536/chatHub?id=p92UpYKLBy761rUOPp-QXA 404 (Not Found)
Utils.ts:168 [2020-10-09T05:51:03.833Z] Error: Failed to start the transport 'ServerSentEvents': Error: Error occurred
ConsoleLogger.log @ Utils.ts:168
(anonymous) @ HttpConnection.ts:373
step @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
rejected @ HttpConnection.ts:2
Promise.then (async)
step @ HttpConnection.ts:2
fulfilled @ HttpConnection.ts:2
Promise.then (async)
step @ HttpConnection.ts:2
rejected @ HttpConnection.ts:2
Promise.then (async)
step @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
__awaiter @ HttpConnection.ts:2
HttpConnection.createTransport @ HttpConnection.ts:339
(anonymous) @ HttpConnection.ts:270
step @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
fulfilled @ HttpConnection.ts:2
Promise.then (async)
step @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
__awaiter @ HttpConnection.ts:2
HttpConnection.startInternal @ HttpConnection.ts:215
(anonymous) @ HttpConnection.ts:126
step @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
__awaiter @ HttpConnection.ts:2
HttpConnection.start @ HttpConnection.ts:113
(anonymous) @ HubConnection.ts:191
step @ HubConnection.ts:2
(anonymous) @ HubConnection.ts:2
(anonymous) @ HubConnection.ts:2
__awaiter @ HubConnection.ts:2
HubConnection.startInternal @ HubConnection.ts:182
(anonymous) @ HubConnection.ts:170
step @ HubConnection.ts:2
(anonymous) @ HubConnection.ts:2
(anonymous) @ HubConnection.ts:2
__awaiter @ HubConnection.ts:2
HubConnection.startWithStateTransitions @ HubConnection.ts:161
HubConnection.start @ HubConnection.ts:157
(anonymous) @ chat.js:36
XhrHttpClient.ts:84 GET http://localhost:15536/chatHub?id=mSOf3cwsos6jVYwVbBT3hw&_=1602222663916 404 (Not Found)
(anonymous) @ XhrHttpClient.ts:84
XhrHttpClient.send @ XhrHttpClient.ts:30
DefaultHttpClient.send @ DefaultHttpClient.ts:39
HttpClient.get @ HttpClient.ts:87
(anonymous) @ LongPollingTransport.ts:78
step @ LongPollingTransport.ts:2
(anonymous) @ LongPollingTransport.ts:2
fulfilled @ LongPollingTransport.ts:2
Promise.then (async)
step @ LongPollingTransport.ts:2
(anonymous) @ LongPollingTransport.ts:2
__awaiter @ LongPollingTransport.ts:2
LongPollingTransport.connect @ LongPollingTransport.ts:46
HttpConnection.startTransport @ HttpConnection.ts:414
(anonymous) @ HttpConnection.ts:369
step @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
fulfilled @ HttpConnection.ts:2
Promise.then (async)
step @ HttpConnection.ts:2
rejected @ HttpConnection.ts:2
Promise.then (async)
step @ HttpConnection.ts:2
fulfilled @ HttpConnection.ts:2
Promise.then (async)
step @ HttpConnection.ts:2
rejected @ HttpConnection.ts:2
Promise.then (async)
step @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
__awaiter @ HttpConnection.ts:2
HttpConnection.createTransport @ HttpConnection.ts:339
(anonymous) @ HttpConnection.ts:270
step @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
fulfilled @ HttpConnection.ts:2
Promise.then (async)
step @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
__awaiter @ HttpConnection.ts:2
HttpConnection.startInternal @ HttpConnection.ts:215
(anonymous) @ HttpConnection.ts:126
step @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
__awaiter @ HttpConnection.ts:2
HttpConnection.start @ HttpConnection.ts:113
(anonymous) @ HubConnection.ts:191
step @ HubConnection.ts:2
(anonymous) @ HubConnection.ts:2
(anonymous) @ HubConnection.ts:2
__awaiter @ HubConnection.ts:2
HubConnection.startInternal @ HubConnection.ts:182
(anonymous) @ HubConnection.ts:170
step @ HubConnection.ts:2
(anonymous) @ HubConnection.ts:2
(anonymous) @ HubConnection.ts:2
__awaiter @ HubConnection.ts:2
HubConnection.startWithStateTransitions @ HubConnection.ts:161
HubConnection.start @ HubConnection.ts:157
(anonymous) @ chat.js:36
Utils.ts:168 [2020-10-09T05:51:04.015Z] Error: Failed to start the transport 'LongPolling': Error: Not Found
ConsoleLogger.log @ Utils.ts:168
(anonymous) @ HttpConnection.ts:373
step @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
rejected @ HttpConnection.ts:2
Promise.then (async)
step @ HttpConnection.ts:2
fulfilled @ HttpConnection.ts:2
Promise.then (async)
step @ HttpConnection.ts:2
rejected @ HttpConnection.ts:2
Promise.then (async)
step @ HttpConnection.ts:2
fulfilled @ HttpConnection.ts:2
Promise.then (async)
step @ HttpConnection.ts:2
rejected @ HttpConnection.ts:2
Promise.then (async)
step @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
__awaiter @ HttpConnection.ts:2
HttpConnection.createTransport @ HttpConnection.ts:339
(anonymous) @ HttpConnection.ts:270
step @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
fulfilled @ HttpConnection.ts:2
Promise.then (async)
step @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
__awaiter @ HttpConnection.ts:2
HttpConnection.startInternal @ HttpConnection.ts:215
(anonymous) @ HttpConnection.ts:126
step @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
__awaiter @ HttpConnection.ts:2
HttpConnection.start @ HttpConnection.ts:113
(anonymous) @ HubConnection.ts:191
step @ HubConnection.ts:2
(anonymous) @ HubConnection.ts:2
(anonymous) @ HubConnection.ts:2
__awaiter @ HubConnection.ts:2
HubConnection.startInternal @ HubConnection.ts:182
(anonymous) @ HubConnection.ts:170
step @ HubConnection.ts:2
(anonymous) @ HubConnection.ts:2
(anonymous) @ HubConnection.ts:2
__awaiter @ HubConnection.ts:2
HubConnection.startWithStateTransitions @ HubConnection.ts:161
HubConnection.start @ HubConnection.ts:157
(anonymous) @ chat.js:36
Utils.ts:168 [2020-10-09T05:51:04.016Z] Error: Failed to start the connection: Error: Unable to connect to the server with any of the available transports. WebSockets failed: Error: There was an error with the transport. ServerSentEvents failed: Error: Error occurred LongPolling failed: Error: Not Found
ConsoleLogger.log @ Utils.ts:168
(anonymous) @ HttpConnection.ts:288
step @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
rejected @ HttpConnection.ts:2
Promise.then (async)
step @ HttpConnection.ts:2
fulfilled @ HttpConnection.ts:2
Promise.then (async)
step @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
__awaiter @ HttpConnection.ts:2
HttpConnection.startInternal @ HttpConnection.ts:215
(anonymous) @ HttpConnection.ts:126
step @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
(anonymous) @ HttpConnection.ts:2
__awaiter @ HttpConnection.ts:2
HttpConnection.start @ HttpConnection.ts:113
(anonymous) @ HubConnection.ts:191
step @ HubConnection.ts:2
(anonymous) @ HubConnection.ts:2
(anonymous) @ HubConnection.ts:2
__awaiter @ HubConnection.ts:2
HubConnection.startInternal @ HubConnection.ts:182
(anonymous) @ HubConnection.ts:170
step @ HubConnection.ts:2
(anonymous) @ HubConnection.ts:2
(anonymous) @ HubConnection.ts:2
__awaiter @ HubConnection.ts:2
HubConnection.startWithStateTransitions @ HubConnection.ts:161
HubConnection.start @ HubConnection.ts:157
(anonymous) @ chat.js:36
HttpConnection.ts:387 Uncaught (in promise) Error: Unable to connect to the server with any of the available transports. WebSockets failed: Error: There was an error with the transport. ServerSentEvents failed: Error: Error occurred LongPolling failed: Error: Not Found
    at HttpConnection.<anonymous> (HttpConnection.ts:387)
    at step (HttpConnection.ts:2)
    at Object.throw (HttpConnection.ts:2)
    at rejected (HttpConnection.ts:2)
=======================================================================
3 года назад
mhsjaber wrote:
Here is how I setup signalR for my plugin.

In plugin start up:
public class PluginNopStartup : INopStartup
{
    public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
    {
        services.AddSignalR(hubOptions =>
        {
            hubOptions.KeepAliveInterval = TimeSpan.FromMinutes(1);
        });
    }

    public void Configure(IApplicationBuilder application)
    {
        application.UseSignalR(routes =>
        {
            routes.MapHub<ProductUploadHub>("/uploadproducts");
        });
    }

    public int Order => 11;
}


In hub:
public class ProductUploadHub : Hub
{
    private readonly IWorkContext _workContext;
    private readonly IHubContext<ProductUploadHub> _hubContext;

    public ProductUploadHub(IWorkContext workContext,
        IHubContext<ProductUploadHub> hubContext)
    {
        _hubContext = hubContext;
        _workContext = workContext;
    }

    public void UploadProducts()
    {
        _hubContext.Clients.All.SendAsync("dataSent", mydata);
    }
}


In cshtml:
$(document).ready(function () {
    var connection = new signalR.HubConnectionBuilder()
            .withUrl('/uploadproducts')
            .build();

    connection.on('dataSent', res => {
    });

    function start() {
        connection.start().catch(function (err) {
            setTimeout(function () {
                start();
            }, 10000);
        });
    }

    connection.onclose(function () {
        start();
    });

    start();
});

hi this solution not work in version 4.3 and above
3 года назад
please help me if any one register signalr hub in version 4.3 i have this error

Could not load file or assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.