Search code examples
asp.net-core.net-coreazure-service-fabric

Azure Service Fabric Dyanamic Port Conflict


I have been wondering why Service Fabric does not take into account that ports clash and re-hosts the service on a diffrent port.

I have multiple application packages that are used at runtime to create new services with unique names, now I know i can probably do some fancy footwork to check for ports numbers in use etc, but i was wondering if there was a simpler solution without chnanging the way I create new services?

To start the service usings ingle packege, give it a uniqe name, and have SF give it a dynamic port without trying to use the same port on the same node as the other package.

eg: packagename : mytestpackage create a new service : mytestService1 create a new service : mytestService2

By chance they both get started on the same node, with the same port number, even if there was a significant time between startup.


Solution

  • I solved my problem by changing my port in the Service Manifest to zero where it had previously been left blank.

    <Endpoint Protocol="http" Name="EndpointHttp" Type="Input" Port="0"/>
    

    The reason behind this is that apparently if the port is left out or blank, Service Fabric tries to allocate the port, where as if you set it to 0, this value is passed to the kestrel server.

    Passing 0 to the kestrel server then allows the host OS to determine which ports are available and to then assign them.

    Doing this has also sped up my service startup time.