Create a Service Fabric Application on demand

"Hello World"!
You probably already know that the Service Fabric SDK allows a developer to fully manage the cluster, the nodes, and the deployed applications by code.

One of the features I like most of the Service Fabric programming is the ability to dynamically create and deploy new services. Or more in general the ability to manage the lifecycle of the running services.

For example, If we want to deploy a new instance of an Application we just need few lines of code:

 FabricClient fabricClient = new FabricClient();
Uri eventDeploymentUri = new Uri("fabric:/NewApp/");           
          
//check for existence
await fabricClient.QueryManager.GetApplicationListAsync(eventDeploymentUri);
if (applicationList.Count > 0) return; //already exists

//passing manifest parameters to the new application
NameValueCollection appParameters = new NameValueCollection();
appParameters.Add("Param1","1" );

ApplicationDescription applicationDesc = 
   new ApplicationDescription(eventDeploymentUri, "APP_TYPE", "APP_VERSION", appParameters);
await fabricClient.ApplicationManager.CreateApplicationAsync(applicationDesc);

 

This is super easy, we just ask the creation of a specific version of a registered application type (the type must be already registered).
The cluster will take care of this operation in an async way and will manage all the tasks related to this new deployment (like moving other services, leveling the volume load, register the name in reverse proxy ..etc..).

This dynamic operation is really useful if we want to build a multi-tenant or self-service solution that can instantiate multiple applications on-demand.
To demonstrate this, I created a sample demo project that builds websites on demand with just a simple REST api call.
In such demo the dynamically built application hosts an asp.net core website with a single page. The website is externally reachable as it is registered with the reverse proxy (read this post for more info about this point).

Hope you will find this demo solution helpful to start with service fabric SDK programming.