每隔几秒就实现SIgnalR来检查数据库中的数据

时间:2016-12-19 19:42:26

标签: c# asp.net signalr asp.net-web-api2 signalr-hub

我已经使用现有的Web Api应用程序实现了SingalR,其中SignalR hub类将每隔几秒检查一次数据库,并根据连接ID更新连接的客户端。

这是我的集线器课程:

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();
        }
    }
}

问题我面对的是, 我们知道Client Application将创建中心实例并建立连接。当我一次运行客户端应用程序时,我没有遇到任何问题。 当我在两个或多个并行运行客户端应用程序时,我遇到了一个问题,这意味着数据完全搞砸了。

例如:对于PushNotificationData方法,客户端应用程序将动态传递userID。在上面的代码中,我在userID ==3143时编写了一个逻辑,响应应该经过验证&#39;,否则&#39;未经过验证&#39;。

如果我在一个浏览器中运行应用程序,我每隔6秒就会收到一次响应,并且已经过验证&#39;

但是,如果我在两个或更多平行地运行应用程序,我会每隔6秒获得一次响应,因为某些时候已经过验证&#39;有些时候没有经过验证&#39;。

我想有些事搞砸了。请帮助我。我希望它清楚。

1 个答案:

答案 0 :(得分:0)

集线器生命周期为"per request".

所以你不应该在hub中保存任何状态并且当然要调用异步循环功能。 尝试按如下方式重写代码:

  • 在您的应用启动期间创建单独的周期(靠近app.MapSinglaR()或类似的东西)。
  • 创建静态并发字典或列表或任何您需要的内容,并使用它来存储活跃用户。
  • 使用GlobalHost.ConnectionManager.GetHubContext<NotifyHub>()
  • 在您的异步周期内获取Hub实例