0

I came here reaching for your help. I am fairly new when it comes to SignalR and after several days of reading and retrying stuff again and again I have reached a dead end. Straight to the point. I have a solution which includes several projects in it. The ones that are giving me trouble are the Web and a class library. I have SignalR installed in the Web project and the SignalR Client in the Class Library. My target is to track and monitor the progress of Long running processes that are running in the Class Library. I have SignalR working properly from the Web. This is my Hub method that I am trying to call:

public void SendProgress(string progressMessage, int progressCount, int totalItems)
    {
        var percentage = (progressCount * 100) / totalItems;

        Clients.All.sendProgress(progressMessage, percentage + "%");
    }

Moreover I have a method in the Class Library (ImportProcess) which contains a foreach loop. Inside that foreach loop I am calling:

public void SendProgress(string message, int currentItem, int totalItems)

The body of the SendProgress is this:

public void SendProgress(string message, int currentItem, int totalItems)
    {
        var hubConnection = new HubConnection("http://localhost:13310/");
        IHubProxy statementsHubProxy = hubConnection.CreateHubProxy("statementsHub");
        hubConnection.Credentials = CredentialCache.DefaultNetworkCredentials;
        hubConnection.Start().Wait();

        statementsHubProxy.Invoke("SendProgress", message, currentItem, totalItems);

    }

The problem is that since I am calling the SendProgress inside a foreach, I consider it wrong to define the connection everytime that SendProgress is called. I tried including the Connection properties to the constructor of the class. It worked but it only returns the first iteration of the foreach and ignored all the others.

I have to mention though, that if I have the connection properties inside the SendProgress method, the code works and I am receiving the information I want in the cosnole, but again, I think it shouldn't be like that.

This is the MVC controller which calls a rest service which in turn calls the class library

public JsonResult AProcess()
    {
        _dataImportService.DataImportProcess("2018-02-27", "2018-02-28", "This is another parameter");
        return Json("", JsonRequestBehavior.AllowGet);
    }

Please ignore the parameters of the DataImportProcess as they are irrelevant at this point.

This is my Startup.cs

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.MapSignalR();
    }
}

I am using SignalR 2.2.2

Edit #1 I included the connection properties to the constructor of the class again like this:

static IHubProxy _proxy { get; set; }
    public ImportClass()
    {
        var hubConnection = new HubConnection("http://localhost:13310/");
        hubConnection.Credentials = CredentialCache.DefaultNetworkCredentials;
        IHubProxy statementsHubProxy = hubConnection.CreateHubProxy("statementsHub");
        _proxy = statementsHubProxy;
        hubConnection.Start().Wait();
    }

But no luck. SendProgress method is not sending any updates at all.

Any help is much, much appreciated. Best regards, Konstantinos.

codinator
  • 1
  • 1
  • 5
  • You're correct in thinking that you should not create the connection inside the foreach loop - normally you subscribe a client to hub and "reuse" that subscription every time you want to send a message from the client to the server. Just to clarify - have you tried setting `statementsHubProxy` as an static property and calling `hubConnection.Start().Wait();` only in the class constructor? – maurcz Feb 08 '18 at 01:20
  • @mauriciorcruz I tried what you proposed with no luck at all. Please see Edit #1. Thank you – codinator Feb 08 '18 at 09:48
  • Did you try enabling SignalR trace? - https://learn.microsoft.com/en-us/aspnet/signalr/overview/testing-and-debugging/enabling-signalr-tracing#enabling-tracing-in-the-net-client-windows-desktop-apps – maurcz Feb 08 '18 at 14:46
  • @mauriciorcruz I just did. I am getting lots of lines in the output window. Am I looking for something specific? I can see that the connection is starting and everything but I don't know for what to look for. – codinator Feb 09 '18 at 09:05
  • I would let `hubConnection.Start().Wait();` inside the constructor and then I would check the log right before `Clients.All.sendProgress(progressMessage, percentage + "%");` to make sure the connnection is opened. If you don't see any entries for the call inside SignalR's trace (after `sendProgress` is fired), then it must be a problem with how you've setup the hub – maurcz Feb 09 '18 at 15:28
  • @mauriciorcruz I will be checking this shortly. Btw. I noticed that the transport protocol is serverSentEvents instead of websockets. Could this cause something? – codinator Feb 09 '18 at 16:08
  • SignalR will decide which transport protocol to use depending on your setup. Here's a handy diagram (use the Updated one) https://stackoverflow.com/questions/16983630/how-does-signalr-decide-which-transport-method-to-be-used – maurcz Feb 09 '18 at 19:30
  • @maurcz I figured out something and trying to work around it. I noticed that when I include all the connection properties in the constructor of the class, I am receiving all the results when rest call is done calling the specific class. I forgot to mention that from the front-end of my application I am calling the classes on the back-end using a rest api call. Does this provide any clue of what could be causing the issue? Thanks! – codinator Feb 12 '18 at 15:25

0 Answers0