SignalR Hub Issue - Restarting iis creates a new hub instance - asp.net

I am using asp.Net SignalR Dependency injection technique for our implementation, so in our case, we have a hub constructor with one parameter. I was wondering if there is a problem I have with the following, or is this how the system should react?
When I restart IIS, signalR will lose connection of course, and then come back online. Afterwards, when it reconnects to our SignalR hub, it attempts to connect to the parameterless Hub constructor instead of the constructor with one parameter that is setup via the NinjectDependecnyResolver. It seems to be trying to connect to the default Hub DependcyResolver after I restart IIS and signalR reconnects. If I refresh the page after the IIS refresh, however, the paramaterized constructor is called, as it should be.
Does anyone know why my provided constructor is not fired after reconnecting to the SignalR hub after an IIS restart/IIS Stop-Start?

Related

storing signalr hub connection details in local storage

I am using signalr client in my application. I want to establish the connection and use the same in all the pages. I could get the connection. To access the hub details i am trying to store it local storage.
Below is the hub details i am getting once i build it.
localStorage.setItem("thisConnectionHub",JSON.stringify(thisConnectionHub));
It is stored as below.
HubConnection is missing in this. Because of this i could not listen to any methods from server.
Any ideas how can i get this work.
Thanks,
If you want to use the same connection, you need to create a service that can be #injected in your components so you don't lose the context and can invoke the client side methods that instead will invoke the hub methods. This is called dependency injection. Your service will establish the connection on the application init and then other components would use the hubConnection methods that you will declare public or event implement an interface to do that.

SignalR request pipeline, Hub lifetime

I am starting on signalR with asp.net webapi, and I am trying to understand the whole signalR request/connection pipeline and what is the lifetime of the hub and who creates and dispose the hub instance (does it gets created on every communication between client and server?)
One of the reason is that we need to understand how we should be using an IoC contain in signalR request scenario, how we control the lifetime of the dependency correct specifically the ISession or DBContext.
Hubs instances are created at every single request from a client. They are not kept around, therefore you should not put any state in them.
Hubs are created through a factory system which you can customize through its built-in dependency injection mechanism.

SignalR in multiple instances in Azure

I'd like to use Azure to host my web application, for instance CloudService web role or Azure Websites, inside the application I use SignalR to connect client and server.
Since I scaled two instances for my web roles, it seems I came across a very common problem, the SignalR could not find the correct original instance. The client JavaScript said it was already started, but the server hub OnConnected event randomly not raised, so were the server methods which intended to be called by clients, all these strange issues happened randomly.
Once I changed the instance to be one, all the problems gone. So can anyone explain what happened when the client call server method, why sometimes the server seems not response properly?
I found the post, can Azure Service Bus solve this issue?
Yes, you need to use the azure service bus. Otherwise the connections are stored in memory on the given server and the other server will know nothing about them. Once you create the service bus, just reference it in the startup class.
public void Configuration(IAppBuilder app)
{
System.Diagnostics.Trace.TraceInformation("SignalR Startup > Configurtion start");
// Any connection or hub wire up and configuration should go here
string connectionString = "XXX";
GlobalHost.DependencyResolver.UseServiceBus(connectionString, "TopicName");
...
}
You will also need to get a reference to the context in each of your hub methods:
var context = GlobalHost.ConnectionManager.GetHubContext<HubName>();
It's easy peasy :)

SignalR- Removing a Client From signalR Hub

Is it possible to remove a client from signalR hub using its connectionId? I'am trying to create an application in which I can remove a client from hub?
SignalR does not provide an API to disconnect clients. You could workaround it by defining a client method and in the body call connection.stop()
SignalR version 2 does not have a built-in server API for disconnecting clients. In the current SignalR release, the simplest way to disconnect a client from the server is to implement a disconnect method on the client and call that method from the server. The following code sample shows a disconnect method for a JavaScript client using the generated proxy.
var myHubProxy = $.connection.myHub
myHubProxy.client.stopClient = function() {
$.connection.hub.stop();
};
Security Note: Neither this method for disconnecting clients nor the proposed built-in API will address the scenario of hacked clients that are running malicious code, since the clients could reconnect or the hacked code might remove the stopClient method or change what it does. The appropriate place to implement stateful denial-of-service (DOS) protection is not in the framework or the server layer, but rather in front-end infrastructure.
More details here

SignalR send message to Clients from external application

Is it possible to send a message to the clients connected to a Hub from another process? I set up LINQPad to reference my Hub project's DLL and set VS to attach debugging to the LINQPad process. My project has a HubNotification class that uses the following code:
dynamic clients = Hub.GetClients<MyHubClass>();
clients.SendMessage("My Message");
When debugging I can see this code being called, but my connected clients never get sent a message. I have verified with Fiddler that there is nothing HTTP happening when the above code runs. Am I missing something or is this just not possible?
That call only works if you're in the same app domain. If you want to connect to the signalr service from another process/application, then you need to use a signalr client. More info here:
https://github.com/SignalR/SignalR/wiki/SignalR-Client-Hubs

Resources