SignalR .NET Client: Cannot Start Connection From Within Web Application App Domain - signalr

I'll try to make a long story short:
I want to call a method on a hub in a web application from a core assembly in my application. Reason being is that I have a number of applications that all eventually call into core and trigger events (think mobile web, admin site, api, etc). I want to notify users of the desktop site of events as they happen, using SignalR.
To spike this, I created a 4-project solution. A core project with the hub proxy. A web app with the hub. Another web app without the hub. And finally a console app.
If I call into core from the console app, and try to send a message to hub clients using the proxy, everything works great. However, if I try to call into core and use the proxy from one of the web apps, the execution hangs at the call to connection.Start():
var connection = new HubConnection("...");
var hub = connection.CreateProxy("Spike.Hub");
connection.Start().Wait();
Is the SignalR.Client stuff not meant to be used from within a web app's app domain? Why does it work as expected in the console app, but not from a web app?
Update: I hooked my spike into the SignalR source and when running from one of the web apps, execution hangs at line 130 of SignalR.Client.Connection. The _syncContext for such a connection is an instance of AspNetSynchronizationContext, and when calling its Post() method, everything stops.

There's a bug in the SignalR.Client and you found it :)

Related

Replicating InteractiveBrowserCredentials authentication flow in Blazor

I wish to upgrade a console application that uses Azure.ResourceManager to report information about subscriptions and resources in an arbitrary Azure subscription. The console application uses the InteractiveBrowserCredentials to pop up a browser window where users can log in (or, most usually, select the applicable Microsoft account they have previously signed into). The application now requires a more interactive front-end, and porting it to Blazor WASM appeared simple; however, at runtime, the following minimal code fails in Blazor WASM:
var armClient = new ArmClient(new InteractiveBrowserCredential());
var subs = armClient.GetSubscriptions().ToArray();
It throws an exception because Monitors can not await on the WASM runtime. InteractiveBrowserCredential authentication failed: Cannot wait on monitors on this runtime.
It is not feasible to add an App Registration to the Azure AD in advance to make this work due to the generic utility nature of the tool; the user should interactively authenticate during the running of the utility.
The console application (and applications like Visual Studio Code) prompts for the necessary credentials at runtime.
What is the best way to replicate the InteractiveBrowserCredential flow from a standalone WASM application?

Azure - Sending data from IoT Hub to Web App Backend

I'm searching for a solution to get data from the Azure IoT Hub to the backend of a Web App also hosted in Azure which is written in ASP.NET 4.6.
It would be best to just receive the raw Json string as fast as possible.
I found others suggesting using Webhooks or Azure functions for a similar purpose but the delay these solutions bring aren't really acceptable.
It would be best to just connect directly to the IoT endpoint and get every message as it comes in. Can anybode please point me to the right direction?
You can simply use the EventHub .NET SDK in your web app, connect to the EventHub-compatible endpoint of the IoT Hub and directly consume the events in your app. This has minimal delay and involves no extra components.
How to guide (.NET core but same applies to .NET Framework): https://learn.microsoft.com/en-us/azure/event-hubs/event-hubs-dotnet-standard-getstarted-send#receive-events
var eventProcessorHost = new EventProcessorHost(
EventHubName,
PartitionReceiver.DefaultConsumerGroupName,
EventHubConnectionString,
StorageConnectionString,
StorageContainerName);
// Registers the Event Processor Host and starts receiving messages
await eventProcessorHost.RegisterEventProcessorAsync<SimpleEventProcessor>();
The Azure SignalR Service can help to broadcast messages to the Web App instances.
There are no direct integration between the Azure IoT Hub and Azure SignalR Service. Basically you can use two patterns for this integration such as PULL-PUSH and PUSH-PUSH.
The following screen shows these integration patterns:
Note, that the PUSH-PUSH pattern with the Azure Event Grid is suitable for solution when the subscriber (consumer) is not critical for processing events in the order.

Send SignalR notification from other process

I have an architectural question with signalR
Let's say I have a web app (pure front JS) that use a web api and the web app query an API that do a long task and want to be notified when the task is finished.
So the web api create a fire and forget task, and we use SSE with signalR to notify the web app. It's working. Great, thanks to signalR.
But now, I want the long task to be run in another process, let's say with a msmq based system.
So, the web app query the API, the web api create a message, and the msmq service process the message asynchronously.
Can the msmq service hosted in another process (maybe another machine !) notify the web app that the task is finished ? It can be possible to put the connection id in the message, but the service can be able to send the notification ?
I would use a servicebus, you can then use this library to forward the message directly to the clients.
https://github.com/AndersMalmgren/SignalR.EventAggregatorProxy

Execute Web Jobs from ASP.Net MVC

I have c# console application which is using for some long running task. In my local system I am executing it from ASP.Net MVC 5 controller by System.Diagnostics.Process class. Now we are going it to implement it into azure as our site is deployed in azure in development mode.
I am new to azure so don't how to do it.However study several article I have found that I can upload my console application as web jobs. I can run web jobs as trigger i.e. ondemand.
But now my question is how can execute this web job from MVC controller as I need to pass some argument from controller?
Currently there is no direct link between WebSite and WebJob, even though they are executed in the same application pool. And the best way would be to post a message to a queue from you MVC app. And on the other end have your WebJob to check for the queue for new messages. Just like Andres already said.
This will not be instant, but easy to implement and cheap.
If you need instant reaction from your console app, you'll need to implement your background tasks as Worker Roles and deploy as a separate VMs, and have there some sort of network communication going on, so you can always reach out for your worker role via TCP.
You can set up your web site to push an item to an Azure Queue, and then have your web job be triggered every time an item is pushed to the queue.
There is some information, including code samples for how to do that on http://www.asp.net/aspnet/overview/developing-apps-with-windows-azure/getting-started-with-windows-azure-webjobs.

SignalR for updating clients on events passed from another Event system

We have this Pub/Sub system that you subscribe to via a callback mechanism in C# to recieve events from various things that happen within the database. This subscription has a callback signature attached to it that allows for the Pub / Sub system to callback any subscribers it has and notify them of the change on that Callback thread.
We are taking our windows application and migrating it into a web application. In doing so, I need a way to update this Web Application (The clients) with information from this Pub / Sub. I want to use SignalR, but not sure where to host it. I assume if I host it on the same Web Application as the Client, it won't be able to subscribe to the pubsub due to it not being able to do background threading.
Currently, I have it in a Console application hosting the SignalR server on a specific port. Obviously this is for testing and not ideal for a larger scale.
My question is.. is it really safe to be hosting SignalR outside of IIS? should I put this in a Windows Service? Web Service somehow? Can it go in a Web Application somehow?

Resources