Communication between a WebJob and SignalR Hub - asp.net-mvc-5

I have the following scenario:
I have an azure webjob (used to send mails) and I need to check the progress of the webjob in my web application.
I am using SignalR to communicate with clients from my server.
When I want to send an email, I push a message in the queue and the azure webjob does his job.
The question is, how can I communicate the progress of the webjob to the client? Originally my idea was to push a message from the webjob, so the Hub could read it from the queue. Then, I would notify clients from the hub. However, I am not able to find a way to communicate the webjob and the hub, I do not know how to trigger an action in the hub when a message is pushed into the queue or in the service bus. That is to say, I dont know how to subscribe the hub to a certain message of the queue.
Can someone help me with it?

The way that I have done it is to set up the webjob as a SignalR client, push messages via SignalR from the webjob to the server, then relay those messages to the SignalR web clients.
Start by installing the SignalR web client (nuget package ID is Microsoft.AspNet.SignalR.Client) on your webjob.
Then in your webjob, initialise your SignalR connection hub and send messages to the server, e.g.:
public class Functions
{
HubConnection _hub = new HubConnection("http://your.signalr.server");
var _proxy = hub.CreateHubProxy("EmailHub");
public async Task ProcessQueueMessageAsync([QueueTrigger("queue")] EmailDto message)
{
if (_hub.State == ConnectionState.Disconnected)
{
await _hub.Start();
}
...
await _proxy.Invoke("SendEmailProgress", message.Id, "complete");
}
}
Your SignalR server will receive these messages and can then relay them to the other SignalR clients, e.g.:
public class EmailHub : Hub
{
public void SendEmailProgress(int messageId, string status)
{
Clients.All.broadcastEmailStatus(messageId, status);
}
}

Related

Azure function with Service Bus trigger calls the namespace with no reason

I have the following code:
[FunctionName("myFunc")]
public static void Run([ServiceBusTrigger("myQueue", Connection = "ConnectionString")]string myQueueItem, ILogger log)
{
log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
}
It is published in an Azure Function App that has Managed Identity configured to a namespace. The app shows no executions (no messages have been sent to the queue/namespace):
Yet, at the same time the namespace shows it has received requests:
The namespace has no other queues and NOTHING else connects/accesses/requests the namespace and queue. Also, if the function app is stopped the requests stop, as well.
I'm trying to figure out why would the function app send requests to the namespace (and so many) when it wasn't even triggered.
At least one reason that I can think of is that it could be monitoring the queue length.
Azure Functions will auto-scale up/down based on the number of messages in the queue after all.

Azure Function is not triggerd after putting a message to a queue in service bus

I have a queue in a service bus. After putting a message into a queue an azure logic app and an azure functions should betriggered and process the content.
My Azure logic app is triggered but my azure funcction is not triggered. My code for azure function:
[FunctionName("ReadMEssageFromQueue")]
public static void Run([ServiceBusTrigger("messagequeue", Connection = "AzureWebJobsStorage")]string myQueueItem, ILogger log)
{
log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
}
host json:
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"AzureWebJobsStorage": "******" // connection string of my service bus
}
}
should I set something in service bus queue to send message to both ressources?
Azure Service Bus Queue messages are picked up by only one processor. So I think in your case, the logic app is picking up and consuming the message first and the message is not available for the function to process. You can try temporarily disabling the logic app and letting function pick the message to confirm this.
Ref: azure-service-bus-queue-with-multiple-listeners
You can trigger the Azure function from your logic app (not sure if it'll help your use case), or you can use Azure Service Bus topics as topics support the model where multiple consumers can subscribe to a topic.
The former option might be a better approach for you from cost perspective, as you'd need to use Standard tier of service bus in order to use topics feature, which means additional cost for you over your current setup.
Also, you might want to use some other name for service bus connection string as AzureWebJobsStorage is used for storage account connection string

IoTHubTrigger Azure Function does not trigger

I have an Azure function with the following signature:
[FunctionName("MyFunction")]
[return: EventHub("eventhubname", Connection = "EVENTHUB_OUTPUT_CONNECTIONSTRING")]
public static string Run([IoTHubTrigger("messages/events", Connection = "IOTHUB_CONNECTIONSTRING", ConsumerGroup = "consumergroup")] EventData message, ILogger log)
I am running another Azure Functions with HTTP trigger. When i post to this function, it sends a device message to IoThub. I can see the message coming into the IoT Hub.
My routes have been setup as follows:
My routes have no special queries and i can see them hitting all the endpoints.
I have used the connectionstring from this page:
When i run in debug and when published to Azure, my function does not trigger when sending device messages. I have created 2 consumer groups and I am running 2 partitions.
Any idea what i could change to make this work?

Azure Service Bus Message Subscription stops working after container restart

I am using Microservices deployed on Kubernetes built using asp.net core 2.2. Each microservice is subscribed to different azure service bus "Subscription". In application start up the service add subscription rules (filters) for events that it want to listen to.
Everything works fine and service process messages that it's subscribed to except when i kill the pod and Kubernetes bring back the replica and then service stops processing the messages and they keep on stacking in the queue. Regardless of how much i wait they are not processed even when i restart the pod.
My Startup.cs
public static IServiceCollection RegisterEventBus(this IServiceCollection services, IConfiguration configuration)
{
var subscriptionClientName = configuration["SubscriptionClientName"];
services.AddSingleton<IEventBus, EventBusServiceBus>(sp =>
{
var serviceBusPersisterConnection = sp.GetRequiredService<IServiceBusPersisterConnection>();
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>();
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
return new EventBusServiceBus(serviceBusPersisterConnection, logger,
eventBusSubcriptionsManager, subscriptionClientName, iLifetimeScope);
});
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
services.AddTransient<PaymentProcessedIntegrationEventHandler>();
services.AddTransient<OrderStatusChangedIntegrationEventHandler>();
return services;
}
//Subscribe to Events
private void ConfigureEventBus(IApplicationBuilder app)
{
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
eventBus.Subscribe<PaymentProcessedIntegrationEvent, PaymentProcessedIntegrationEventHandler>();
eventBus.Subscribe<OrderStatusChangedIntegrationEvent, OrderStatusChangedIntegrationEventHandler>();
}
I copied official Azure Service bus class from eshopcontainers from github and using it in my project here.
https://github.com/ImranMA/MicroCouriers/blob/master/src/BuildingBlocks/EventBus/EventBusServiceBus/EventBusServiceBus.cs
Any help is highly appreciated !

Azure service bus dead letter queues

I am using azure service bus topic and subscription mechanism and want to process the messages which are all in the dead letter queue.
Moreover i want to process the messages via azure web job in C# and send them back to queue. So i want to know how I can process the messages on the deadletter queue through my application?
When a message is deadlettered it goes onto the dead letter queue for the subscription from which it was read. You access that just like you'd access the original subscription except that you append /$DeadLetterQueue to the subscription name.
Moreover i want to process the messages via azure web job in C# and send them back to queue.
As spodger pointed that the path of your deadletter subscription would be:
{topic-path}/Subscriptions/{subcription-name}/$DeadLetterQueue
You could use the WebJobs SDK for Service Bus and leverage the ServiceBusTrigger to access your dead letter queue message(s) as follows:
public void ProcessDeadletterQueue(
[ServiceBusTrigger("topicName", "subscriptionName/$DeadLetterQueue")] BrokeredMessage message)
{
//TODO:
}
For more details, you could refer to here.
When a message is dead-lettered from a Service Bus Entity(Queue or Topic Subscription), it will be moved to the dead-letter path of the same entity. The reason for dead-lettering will be available in the message's custom properties DeadLetterReason and DeadLetterErrorDescription.
In order to receive the dead-letter messages,
string path = Microsoft.ServiceBus.Messaging.SubscriptionClient.FormatDeadLetterPath(topicPath, subscriptionName);
var subscriptionClient = SubscriptionClient.CreateFromConnectionString(connectionString, path);
BrokeredMessage message = subscriptionClient.Receive();

Resources