locked
SignalR SQL backplane issue RRS feed

  • Question

  • User1409391054 posted

    We have and existing SignalR application and it was requested to use SQL Server as a backplane to make the application more scalable.

    Existing code is using SignalR 2.2.2

    Updated code is using SignalR 2.4.1

    After reading the online documentation I thought it would be as simple as getting the database and account setup and then updating Startup.Configuration() to pass the connection string.
        public class Startup
        {
            public void Configuration(IAppBuilder app)
            {
                string sqlConnectionString = WebConfigurationManager.AppSettings["SignalR_Backplane_ConnectionString"];

                // Get the password from Cyberark and tack it onto the connection string
                .
                .
                .
                sqlConnectionString = sqlConnectionString + cyberArkValue + ";";

                GlobalHost.DependencyResolver.UseSqlServer(sqlConnectionString);
                app.MapSignalR();
            }
        }

    Startup.Configuration() executes without issue.  Below is the code for interacting with SignalR

        private bool SendMessage(string message, string userId, string hubUrl)
        {
            if (string.IsNullOrEmpty(hubUrl))
            return false;
            bool result = true;
            try
            {
            using (HubConnection connection = new HubConnection(hubUrl))
            {
                var myHub = connection.CreateHubProxy("Messages");

                connection.Start().ContinueWith(task =>
                {
                if (task.IsFaulted)
                {
                    Database.Logger(LogType.Error, userId, "Browser", $"Couldn't start a connection to hub at {hubUrl}.", DateTime.UtcNow);
                }
                }).Wait();

                myHub.Invoke("AtfMessage", message).ContinueWith(task =>
                {
                if (task.IsFaulted)
                {
                    Database.Logger(LogType.Error, userId, "Browser", $"Couldn't send message to hub at {hubUrl}.", DateTime.UtcNow);
                    result = false;
                }
                });
            }
            }

            catch(Exception ex)
            {
            Database.Logger(LogType.Error, userId, "Browser", ex.Message, DateTime.UtcNow);
            result = false;
            }
            return result;

    My connection string is:

            "Data Source=serverName;Initial Catalog=XX_ATF_MSG_;User Id=theUsername;Password=thePassword"

    After calling .Start() Microsoft.AspNet.SignalR.Client.Connection.Start returned
            Id = 159, Status = Faulted, Method = "{null}", Result = "{Not yet computed}"

    An exception is thrown when calling myHub.Invoke and the exception message is:
            {"Data cannot be sent because the connection is in the disconnected state. Call start before sending any data."}

    Friday, January 22, 2021 4:07 PM

Answers

  • User-1330468790 posted

    Hi jdherzog, 

      

    It looks like you don't successfully connect with the hub as the state is still 'disconnected'. I notice that the returned value contains 'Status=Faulted' which means => The task completed due to an unhandled exception. which might be the reason why you got failed when invoking the method.

      

    I suggest you firstly check the codes to see if there is anything that you have changed accidently since the previous version was working.

    The second thing is to check whether you have updated the signalR for both server and client side.

    Lastly, enabling SignalR tracing would be much helpful to debug your signalR project.

    More details, you could refer to below documentation:

    https://docs.microsoft.com/en-us/aspnet/signalr/overview/testing-and-debugging/enabling-signalr-tracing

      

    Hope helps.

    Best regards,

    Sean 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 25, 2021 6:43 AM

All replies

  • User-1330468790 posted

    Hi jdherzog, 

      

    It looks like you don't successfully connect with the hub as the state is still 'disconnected'. I notice that the returned value contains 'Status=Faulted' which means => The task completed due to an unhandled exception. which might be the reason why you got failed when invoking the method.

      

    I suggest you firstly check the codes to see if there is anything that you have changed accidently since the previous version was working.

    The second thing is to check whether you have updated the signalR for both server and client side.

    Lastly, enabling SignalR tracing would be much helpful to debug your signalR project.

    More details, you could refer to below documentation:

    https://docs.microsoft.com/en-us/aspnet/signalr/overview/testing-and-debugging/enabling-signalr-tracing

      

    Hope helps.

    Best regards,

    Sean 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 25, 2021 6:43 AM
  • User1409391054 posted

    Thanks.  I implemented the chat tutorial and that is working, which lets me know my DB account and connection string are working.  I'll add tracing and see what it shows.

    Monday, January 25, 2021 8:15 PM
  • User1409391054 posted

    I went back to the original code and made code changes cautiously.  I only made a couple of changes and I was able to get our code to work with SQL Server acting as a backplane for the messages.  It seems that choosing to use SignalR 2.4.1 at the solution level resulted in the odd behavior I was seeing.

    Tuesday, February 2, 2021 5:43 PM