简体   繁体   中英

Signalr N-Layers / Class library get updates from Server to Clients

I am new with this (Signalr) and I been reading about this but, i don't find, and i don't know if i do the things the better form.

What i want do is from my class library Notifications sends update to the client when from a windows service send an insert into database, so I do the insert and call the hub (class library Notifications) to which you send the notice to the customer and Real-time updates about graphics

I make the class library Notifications, install from nuget signalr client and component, and build all but this not work, this is the code:

This is the method in Project Windows service:

public void ProcessResults()
        {
            RuleDto ruleToExecute;
            using (var svc = new ServiceWrapper<ICentralMonitor>(ConfigurationManager.AppSettings["MonitorBinding"]))
            {
                 ruleToExecute = svc.Channel.GetRulesToApplyByValidationGroup(Id, _results.ToDictionary(m => m.Key.Id, pair => pair.Value));
            }

            if (ruleToExecute != null)
            {   
                foreach (var action in ruleToExecute.Actions)
                {
                    object[] parameters = { Id };
                    InvokeHandler.InvokeAction(action.Assembly, action.Namespace, action.Class, action.Method, parameters);
                }

                **NotificationHub notifier = new NotificationHub();
                notifier.NotifierInsertExecutionRule(Id);**
            }

            UpdateCurrentState();
        } 

This is the Project class library Notifications: (This is all what have this project)

public class NotificationHub : Hub
    {
        public async void NotifierInsertExecutionRule(long id)
        {
            var stats = await StatisticsController.CreateModelAsync(id);
            hubContext.Clients.All.updateStatistics(stats);
        }
    }

And in the Project web (Client), I install from Nuget signalr in this project too.

i just put alert to see this work, but don't do anything.

<script type="text/javascript">
    var connection = $.hubConnection();
    var hub = connection.createHubProxy("NotificationHub");
    hub.on("updateStatistics", function (statistics) {

        alert("Se Actualizo");

    });

    connection.start();
</script>

And the startup:

using Microsoft.Owin;
using Owin;

[assembly: OwinStartup(typeof(ADC.Monitor.Monitor.Web.App_Start.Startup))]
namespace ADC.Monitor.Monitor.Web.App_Start
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Any connection or hub wire up and configuration should go here
            app.MapSignalR();
        }
    }
}

Thanks in advance

The process results is creating a HubContext that's not the one that your client connected to. Also try invoking the Hub from the client to at least see if that's working, all your client is doing is listening. IE :

hub.invoke("notifierInsertExecutionRule", someId);

also to access your hubcontext from outside the hub you have to do something like this.

var hubContext = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
hubContext.Clients.All.updateStatistics("Message");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM