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.
Open Visual Studio and create a new Service Fabric application.
Choose the Stateless ASP.NET Core template.
Choose Web API.
Open the ServiceManifest.xml file.
Note the name of the endpoint specified for your service. Default is
ServiceEndpoint
.Open the ApplicationManifest.xml file
In the
ServiceManifestImport
element, add a newRessourceOverrides
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>
In the
Endpoint
element, you can now override any attribute using a parameter. In this example, you specifyPort
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>
Still in the ApplicationManifest.xml file, you then specify the parameter in the
Parameters
element<Parameters> <Parameter Name="MyWebAPI_PortNumber" /> </Parameters>
And define a
DefaultValue
<Parameters> <Parameter Name="MyWebAPI_PortNumber" DefaultValue="8080" /> </Parameters>
Open the ApplicationParameters folder and the
Cloud.xml
fileTo 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.