Website slowness when using Signalr for notifications

Asked

Viewed 145 times

0

Good morning, I’m developing a website to run in local intranet format on IIS. With technologies asp.net ADO in format WebForms, I’m using the library SignalR to monitor a simple table on SQL Server Express via SqlDependency, that shows customers equal real-time notifications from the top of the stackoverflow site. Users are authenticated on the site via authentication Forms, the implementation is functional but when startar the site it will be slow and slow until the lock. I imagine the flow of calls to the bank is jamming the network/OSI. Look at the implementation code, if anyone can point out what is causing the slowness.

Hub class:

using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;

namespace ZaniADV
{
    [HubName("MyHub")]
    public class MyHub : Hub
    {
        public static void Show()
        {
            IHubContext context = 
                GlobalHost.ConnectionManager.GetHubContext<MyHub>();
            context.Clients.All.displayStatus();
        }
    }
}

Statup class:

using Microsoft.Owin;
using Owin;
using Microsoft.AspNet.SignalR;

[assembly: OwinStartup(typeof(ZaniADV.Startup))]

namespace ZaniADV
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {            
            GlobalHost.Configuration.DefaultMessageBufferSize = 500;

            app.MapSignalR();
        }
    }
}

Global.asax

namespace ZaniADV
{
    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            SqlDependency.Start(ConfigurationManager.ConnectionStrings
                ["MyConnectionString"].ConnectionString);
        }

        protected void Application_End()
        {
            SqlDependency.Stop(ConfigurationManager.ConnectionStrings
                ["MyConnectionString"].ConnectionString);
        }
    }
}

Code-Behind page Menunotification.aspx.Cs

        [WebMethod]
        public static IEnumerable<Inbox> GetData()
        {
            using (var connection = new SqlConnection
                (ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString))
            {
                connection.Open();
                using (var command = new SqlCommand(@"SELECT [IndexID],                        
                        [Numero] FROM [AdvogadoDB].[dbo].[IndexA] WHERE [AdvogadoID] =" + 
                        AdvogadoCRUD.ReturnAdvogadoID((Guid)Membership.GetUser().ProviderUserKey),
                        connection))

                {
                    command.Notification = null;

                    SqlDependency.Start(
                        ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
                    SqlDependency dependency = new SqlDependency(command);
                    dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);

                    if (connection.State == System.Data.ConnectionState.Closed)
                        connection.Open();

                    using (var reader = command.ExecuteReader())
                        return reader.Cast<IDataRecord>()
                            .Select(x => new Inbox()
                            {
                                Numero = x.GetInt32(1),                                                   

                            }).ToList();
                }
            }
        }

        private static void dependency_OnChange(
            object sender, SqlNotificationEventArgs e)
        {
            MyHub.Show();
        }
    }
}

Javascript code of the client page Menunotification.aspx:

<script src="Scripts/jquery-3.1.1.min.js"></script>
<script src="Scripts/jquery.signalR-2.2.2.js"></script>
<script src='<%= ResolveClientUrl("~/signalr/hubs") %>'></script>

        <script type="text/javascript">

        $(function () {
            var job = $.connection.MyHub;

            job.client.displayStatus = function () {
                getData();
            };

            $.connection.hub.start();
            getData();
        });

        function getData() {
            var $inbox = $('#inbox');
            $.ajax({
                url: 'MenuNotification.aspx/GetData',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                type: "POST",
                success: function (data) {
                    //debugger;
                    if (data.d.length > 0) {
                        $inbox.empty();
                        var newdata = data.d;
                         var rows = [];
                        for (var i = 0; i < newdata.length; i++) {
                            rows.push(newdata[i].Numero);
                        }
                        $inbox.append(rows.join(''));
                    }
                }
            });
        }

    </script> 

                <li class="dropdown tasks-menu"
                    data-toggle="tooltip" data-placement="bottom"
                    title="Últimas mensagens no inbox">
                    <a href="/ZaniADV/Views/Designar.aspx" target="_parent"
                        class="dropdown-toggle" data-toggle="dropdown">
                        <img src="/ZaniADV/Image/inbox.png" />
                        <label id="inbox" class="label label-danger" 
                            style="font-size: 12px"></label>
                    </a>
                </li>

BD table:

inserir a descrição da imagem aqui

ALTER DATABASE Advogadobd SET ENABLE_BROKER

Final result:

Client 1:

inserir a descrição da imagem aqui

Client 2:

inserir a descrição da imagem aqui

Resource monitor:

Memoriam:

inserir a descrição da imagem aqui

CPU: inserir a descrição da imagem aqui

Network: inserir a descrição da imagem aqui

  • Have you checked what happens when the site slows down? Whether it is because of processing, memory or indeed network traffic?

  • @Leandroangelo the traffic of the TCP protocol gets very high! see the prints added.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.