Last week I spend a lot of time implementing something I thought would be easy, but took me a while in the end. Inside the Azure Service Fabric I have an actor that needs to talk to a web api hosted in a stateless service. To make this work I need to know the web api endpoint uri to call. My first attempt was to use the default HttpClient but this is not such a good idea as the service instances can move between nodes and the uri can change.
So what is the correct way to do this?
First thing we need to do is to create a communication client and implement the ICommunicationClient interface:
This communication client will be created using a communication client factory. The Reliable Services API provides a
CommunicationClientFactoryBase<TCommunicationClient>. This class implements a typical fault-handling retry pattern that makes retrying connections to resolved service endpoints easier.
We have to implement this abstract CommunicationClientFactoryBase class to handle logic that is specific to our communication stack:
Finally, an exception handler is reponsible for determining what action to take when an exception occurs:
Now we can call our Web API endpoint in a safe way: