Can a Service Fabric stateless service notify other instances? - c#

I have an Azure Service Fabric application with one stateless service. I have multiple virtual machines running multiple instances of this service.
Is there a way for one instance to notify all other instances that something happened? I was thinking about using Azure Service Bus to subscribe all instances for some topic, and raise a message when an event occurs. Is there a better way?

Related

Azure Service Bus Connections

I have inherited an azure service bus solution - C#, Web Api with Singleton service implementing the queue. Running locally on my PC, I can publish a message to my Dev queue and see that event consumed by my service bus receiver. No problem.
In our staging environment however my receiver is not firing so my code never processes the messages. I found an instance where a different environment was pointing to the staging queue purely by luck which makes me think "what else is using this queue". We have no application logging (useless I know) of when events are published or consumed so I wondered, is there a way from within Azure to see either
What is consuming the events published to the queue, or
What is currently connected to the queue so I can validate each connection and make sure a dev in a far flung office isn't running test programs using the queue.
Thanks
Create application insights instance
Connect your web app in azure to the created AI
after some time you will be able to see requests to other systems sent by your app (in application map you'll see fancy diagram of requests, in logs you can query requests to service bus)
Drop the AI instance if you don't need it anymore

Implementing an Azure Service Bus listener application

I'm developing ASP.NET Web API services and placing these onto an Azure Service Bus queue to be processed. The Web API services are hosted on Azure.
I need to implement an application that listens for these messages and processes them when they are received.
I'd like this to be hosted on Azure but not sure of the best way to approach this.
Can you implement such a listener service and host it on Azure?
What is the best way to approach implementing such an application / service?
There are several things you can do.
You could use ASB's OnMessage API which allows you to register your callback and handle incoming messages with concurrency and auto-completion.
On Azure you have several options: Cloud Services (worker roles), Azure Web Jobs, Azure Functions (if your processing is fast, otherwise I'd not recommend it), Service Fabric (might be a bit of an overkill if system is small), and plain VMs if needs to be.
Warning about functions - if you do intense work, Functions are not ideal as you'll pay for time/memory you execute.
A couple options for workers that listen to a queue are:
Functions
Web Jobs
You can see an example of using a Function here: https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-an-event-processing-function.
An example of using Web Jobs is here: https://learn.microsoft.com/en-us/azure/app-service-web/websites-dotnet-webjobs-sdk-service-bus.
Both allow you to create background jobs that consume messages from a queue. Both support Storage and Service Bus queues. The main difference is that Web Jobs require an App Service Plan with some amount of instances, while Functions can run on a Dynamic plan, which scales completely automatically.
You should note that Functions are not meant for really long-running jobs (more than 5-15 minutes), though neither are Web Jobs.
Why not trying to run a linux process (daemon) in docker.

Unsubscribing events before shutdown of self hosted endpoint

I have a bunch of wpf test clients which are self hosted nservicebus endpoints. The test clients are subscribing to several events of another endpoint at startup. Before the test clients are shutting down, I want their subscriptions to be removed. The first idea I came up with, was to use IWantToRunWhenBusStartsAndStops.Stop() where I unsubscribe all events.
The problem with this solution is that when nservicebus invokes IWantToRunWhenBusStartsAndStops.Stop() some objects have already been disposed. e.g. when nservicebus tries to create an object of TransportDefinition to decide if the endpoint has support for centralized pub\sub.
I'm not sure, if the problem only occurs in a self hosted scenario. Are there any suggestions?

Where should I put my Azure ServiceBus listener

I have created a ServiceBus listener, and it works fine; the problem is deciding where to put it. It's now located in one of my APIs, but when the API is not in use the listener stops listening. I assume that's because of the application lifetime, and that the API is not running when it's not in use.
Can you provide me guidance on how and where to place the Azure ServiceBus listener so that it runs all the time and never goes down?
Sounds like you need a Worker Role running in a Cloud Service. A Worker Role is basically a VM wrapped around your code.
.NET Multi-Tier Application Using Service Bus Queues should get you going.

Signalr on Azure: service bus required for calling a single client?

I read that Signalr on Azure requires a service bus implementation (e.g. https://github.com/SignalR/SignalR/wiki/Azure-service-bus) for scalability purpose.
However, my server only makes callbacks to a single client (the caller):
// Invoke a method on the calling client
Caller.addMessage(data);
If don't need Signalr's broadcasting functionality, is an underlaying service bus still necessary?
The Service Bus dependency is not something specific to Azure. Any time you have multiple servers in play, some of your signalR clients will have created their connection to a specific server. If you want to keep multiple servers in sync something needs to handle the server to server real time communication. The pub-sub model of service bus lines up with this requirement quite well.
dfowleR lists a specific case of this in the comments. Make sure you read down that far!
If you are running on a single server (without the sla on Azure) signalR will work just fine on a Cloud Service Web Role as well as the new Azure Web Sites. I did a screencast on this simple scenario that does not take on a service bus dependency, but only runs on a single server.
In order to support the load balance scenario, is it possible to enstablish a "server to server" SignalR PersistConnection between multiple instances (ie on Azure) ?
If so, we can use a SQL Azure Table where all instances register at startup, so newest can connect to previous ones.

Categories