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.