Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have Implemented SingalR with existing web Api Application, where SignalR hub class will check the database for every few seconds and update the clients which are connected based on connection Id.

here is my hub class :

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using System.Threading.Tasks;
using VHC.Core.Business.Managers.Old;
using VHC.Core.Common.Requests;
using VHC.Core.Business.Interfaces;
using VHC.Core.ServiceProxy.ServiceProxies;

namespace VHC.Core.WebAPI
{
    public class NotifyHub : Hub
    {

        public static string ConnectionId { get; set; }
        public string NotificationResult { get; set; }

        //public NotifyHub()
        //{
    //}


        public void PushNotificationData(Int32 userId, Int16 userTypeId)
        {

            lock (this)
            {
                var request = new PushNotificationRequest
                {
                    FilterProperty = new Common.Filters.FilterProperty { Offset = 0, RecordLimit = 0, OrderBy = "datechecked desc" },
                    Filters = new List<Common.Filters.FilterObject> { new Common.Filters.FilterObject { LogicOperator = 0, ConditionOperator = 0, Function = 0, FieldName = "", FieldValue = "", FieldType = 0 } }
                };
                INotificationManager inotifity = new NotificationManager();
                var taskTimer = Task.Run(async () =>
                {
                    while (true)
                    {
                        //var serviceProxy = new NotificationServiceProxy();
                        // var NotificationResult = inotifity.PublishResultsAsync(request, userId, userTypeId);--check database

                        if (userId == 3134)
                        {
                            NotificationResult = "validated";
                           //Sending the server time to all the connected clients on the client method SendServerTime()
                            Clients.Client(ConnectionId).NotificationToClient(NotificationResult);
                        }
                        else
                        {
                           NotificationResult = "not validated";

                           //Sending the server time to all the connected clients on the client method SendServerTime()
                           Clients.Client(ConnectionId).NotificationToClient(NotificationResult);
                        }

                        //Delaying by 6 seconds.
                        await Task.Delay(6000);
                    }
                } );
            }
        }

        public override Task OnConnected()
        {
            ConnectionId = Context.ConnectionId;

            return base.OnConnected();
        }
    }
}



Problem I'm facing is, As we know Client Application will create hub instance and connection will be established. When I'm running a client application at once, I didn't get any issue. When I'm running the client application at two or more parallely, I'm getting an issue which means data are completely messed up.

eg: for PushNotificationData method, Client application will pass the userID dynamically. In above code , I have written a logic when userID ==3143 ,the response should be 'validated', else 'not validated'.

if i'm running an application in one browser, I'm getting the response every 6 seconds as 'validated'

but if i'm running an application at two or more parallely,I'm getting the response every 6 seconds as sometime 'validated' some time 'not validated'.

I guess something messed up. kindly assist me.

What I have tried:

I tried lock statement can fix my issue, but it doesn't.
Posted
Updated 20-Dec-16 0:04am

1 solution

Hello,

Can you try

C#
Clients.Caller.NotificationToClient(NotificationResult);


Also if you want to map users and connections you should read Mapping SignalR Users to Connections | The ASP.NET Site[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900