How to specify the port number of a service using parameters in Service Fabric

This article shows you how to specify the port number of a service using parameters in Service Fabric using Visual Studio.

Procedure for specifying the port number of a service using parameters

In this example, you set the port number for your asp.net core web API using a parameter.

  1. Open Visual Studio and create a new Service Fabric application.

  2. Choose the Stateless ASP.NET Core template.

  3. Choose Web API.

  4. Open the ServiceManifest.xml file.

  5. Note the name of the endpoint specified for your service. Default is ServiceEndpoint.

  6. Open the ApplicationManifest.xml file

  7. In the ServiceManifestImport element, add a new RessourceOverrides element with a reference to the endpoint in your ServiceManifest.xml file.

      <ServiceManifestImport>
        <ServiceManifestRef ServiceManifestName="Web1Pkg" ServiceManifestVersion="1.0.0" />
        <ResourceOverrides>
          <Endpoints>
            <Endpoint Name="ServiceEndpoint"/>
          </Endpoints>
        </ResourceOverrides>
        <ConfigOverrides />
      </ServiceManifestImport>
    
  8. In the Endpoint element, you can now override any attribute using a parameter. In this example, you specify Port and set it to a parameter name using square brackets - for example, [MyWebAPI_PortNumber]

      <ServiceManifestImport>
        <ServiceManifestRef ServiceManifestName="Web1Pkg" ServiceManifestVersion="1.0.0" />
        <ResourceOverrides>
          <Endpoints>
            <Endpoint Name="ServiceEndpoint" Port="[MyWebAPI_PortNumber]"/>
          </Endpoints>
        </ResourceOverrides>
        <ConfigOverrides />
      </ServiceManifestImport>
    
  9. Still in the ApplicationManifest.xml file, you then specify the parameter in the Parameters element

      <Parameters>
        <Parameter Name="MyWebAPI_PortNumber" />
      </Parameters>
    
  10. And define a DefaultValue

      <Parameters>
        <Parameter Name="MyWebAPI_PortNumber" DefaultValue="8080" />
      </Parameters>
    
  11. Open the ApplicationParameters folder and the Cloud.xml file

  12. To specify a different port to be used when publishing to a remote cluster, add the parameter with the port number to this file.

      <Parameters>
        <Parameter Name="MyWebAPI_PortNumber" Value="80" />
      </Parameters>
    

When publishing your application from Visual Studio using the Cloud.xml publish profile, your service is configured to use port 80. If you deploy the application without specifying the MyWebAPI_PortNumber parameter, the service uses port 8080.

Next steps

To learn more about some of the core concepts that are discussed in this article, see the Manage applications for multiple environments articles.

For information about other app management capabilities that are available in Visual Studio, see Manage your Service Fabric applications in Visual Studio.