If you make a new Service Fabric Web Api application the PackageRoot/ServiceManifest.xml
file has a ServiceManifest/Resource/Endpoints/Endpoint
node with the attribute Port
. Service Fabric uses this to decide the port to host the service on.
But if you delete this attribute, then Service Fabric will dynamically pick a port for you.
I am trying to find a way to get what that port is while the application is running.
I know I could ask the Service Fabric API for a list of the Endpoints for my service and then limit that down by my current node. And that is very close. But if for some reason my service is hosted twice on the same node, then I will not be able to know which one is the "Current" application.
I am hoping there is a way that does not involve a service call to the cluster.
Is the port that is selected stored somewhere that I can access it. (In the StatefulServiceContext
perhaps?
I think I found a way to get this value. It seems as if service fabric adds a bunch of environment variables for the executing process. One of them has the port in it.
This code gave me my port number:
And important thing to note is that the value you use as the environment variable lookup string is dependent on your application's configuration.
The first part (FABRIC_ENDPOINT_) is always the same.
But the rest depends on the value in the config file I mentioned in the question (ServiceManifest.xml). It must match what is in the
ServiceManifest/Resource/Endpoints/Endpoint
Name attribute.