May 27th, 2018

Running .NET Core 2.0 applications in a Linux Service Fabric Cluster on Azure

Developer Support
Cloud Solution Architects

In this post, Senior App. Dev. Managers Guru Satish Piduru and Mahendar Madhavaram explain how to deploy .NET Core 2.0 applications to a Linux Service Fabric Cluster.


The support for running .NET Core 2.0 applications in Service Fabric Cluster (SFC) environment on Linux is in preview mode today and general availability in the coming months. Today, stateless Service Fabric applications are supported in .NET Core 2.0 without any issues. If you are using Microsoft Visual Studio 2017 to develop Service Fabric applications, you can publish the applications directly to Azure SFC running on Windows but if you want to publish the applications to Azure SFC running on Linux, you may run into some issues depending on the project type and template that you choose to create an application.

In this post, we will demonstrate how to deploy .NET Core 2.0 applications to a Linux SFC using scripts until Visual Studio IDE provides this capability.

Setup Local Service Fabric Cluster

To build and run Azure Service Fabric applications on your Windows development machine, install the Service Fabric runtime, SDK, and tools. You also need to enable execution of the Windows PowerShell scripts included in the SDK. Here are instructions to setup your Windows development environment.

Setup Azure Service Fabric Cluster

If you haven’t already, setup the Azure SFC on Linux using the Azure Portal. For the OS version, select ‘UbuntuServer 16.04-LTS’ as shown in the picture below. Please check the following documentation for more information on all other settings.


clip_image002


Walkthrough: .NET Core Stateless Service Fabric Application

Step 1: In Visual Studio 2017, create a new Service Fabric Application

New project > Visual C# > Cloud > Service Fabric Application

clip_image004

Click OK to select a Template ‘.NET Core 2.0 Stateless Service’

clip_image006

Step 2: Update the Runtime Identifier to linux-x64

Open all the service .csproj files and update the RuntimeIdentifier as shown below

<PropertyGroup>

<OutputType>Exe</OutputType>

<TargetFramework>netcoreapp2.0</TargetFramework>

<IsServiceFabricServiceProject>True</IsServiceFabricServiceProject>

<RuntimeIdentifier>linux-x64</RuntimeIdentifier>

</PropertyGroup>


Step 3: Update the ServiceManifest.xml to remove .exe extension from Stateless1.exe to Stateless1 as shown below

<CodePackage Name=”Code” Version=”1.0.0″>

<EntryPoint>

<ExeHost>

<Program>Stateless1</Program>

</ExeHost>

</EntryPoint>

</CodePackage>

Note: If you are building for Windows, the entry point is Stateless1.exe but since you’re building for Linux, it’s not going to produce a Windows EXE file. Therefore, we have to change that in the ServiceManifest.xml to the binary output file that’s being built for Linux (usually it’s the same file name but without the .exe extension).

Step 4: Right click on the Project and click Publish. In the Publish Service Fabric Application wizard, select the Endpoint for SFC cluster and then click Publish.

clip_image002[1]

You can login to the Azure Portal, select Service Fabric Cluster, and verify if the application is deployed successfully.

clip_image010


Walkthrough: ASP.NET Core Stateless Service Fabric Application

Step 1: In Visual Studio 2017, create a new “out of the box” Service Fabric Application

New project > Visual C# > Cloud > Service Fabric Application

clip_image011

Click OK to select a Template ‘Stateless ASP.NET Core’

clip_image013

Click on ‘Web Application (Model-View-Controller)’

clip_image015

Step 2: Update the Runtime Identifier to linux-x64

Open all the service .csproj files and update the RuntimeIdentifier as shown below

<PropertyGroup>

<OutputType>Exe</OutputType>

<TargetFramework>netcoreapp2.0</TargetFramework>

<IsServiceFabricServiceProject>True</IsServiceFabricServiceProject>

<RuntimeIdentifier>linux-x64</RuntimeIdentifier>

</PropertyGroup>

Step 3: Update the ServiceManifest.xml to remove .exe extension from Stateless1.exe to Stateless1 as shown below

<CodePackage Name=”Code” Version=”1.0.0″>

<EntryPoint>

<ExeHost>

<Program>Web1</Program>

</ExeHost>

</EntryPoint>

</CodePackage>

Step 4: Click on Publish won’t work like it did in example ‘.NET Core Stateless Service’ above. This has to do with an issue in VS packaging for Linux for ASP.NET Core projects. As noted above, this will be fixed in an upcoming release.

Here are the steps which should work using dotnet commands to publish.

  1. Only use VS to create application package structure, right click on Applciation3 in solution explorer and select Package. This will create application package structure at c:\user\source\Application3\Application3\pkg\Debug
  2. Open Command Prompt, cd to the directory containing the AspNetCore .csproj (c:\user\source \Application3\Web1) and run following commands:

dotnet build

dotnet publish –self-contained -r ubuntu.16.04-x64 -o c:\user\source \Application3\Application3\pkg\Debug\Web1Pkg\Code

Step 5: Now use Service Fabric PowerShell to create the application.

  1. Connect-ServiceFabricCluster mmlinuxsfc.eastus.cloudapp.azure.com:19000 (Provide security credentials if its secured cluster)
  2. Copy-ServiceFabricApplicationPackage -ApplicationPackagePath c:\user\source \Application3\Application3\pkg\Debug -ApplicationPackagePathInImageStore pkg_app3_cmd
  3. Register-ServiceFabricApplicationType -ApplicationPathInImageStore pkg_app3_cmd
  4. New-ServiceFabricApplication -ApplicationName  fabric:/Application3Type -ApplicationTypeName Application3Type -ApplicationTypeVersion 1.0.0

Note: Register-ServiceFabricApplicationType occasionally times out. Use compression flag for faster uploads.

If Step 5 runs successfully, Application3 should be installed in SFC.

clip_image017

If the Application3 failed to upload or times out, you can use Azure Service Fabric CLI command, sfctl to upload the application. You can install the Azure Service Fabric CLI from https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-cli

After using the “sfctl application upload” and “sfctl application provision”. The ASP.NET 2.0 MVC service, is deployed to Linux SFC and healthy.

C:\users\pathwherphythoninstalled>sfctl

application upload –path c:\user\source \Application3\Application3\pkg\Debug –show-progress

C:\users\pathwherphythoninstalled>sfctl

application provision –no-wait –application-type-build-path Debug

Note: if Application has references to other project which has reference to NuGet packages, use the following workaround:

Compile the above project using the below command and save these binaries:

1. dotnet publish -c Release -r linux-x64

2. In Visual Studio, compile the Service Fabric project to generate the binaries

3. Replace the generated binaries with the ones generated in #1

4. Use SF CLI to upload the binaries and create the application

The application that has NuGet package reference has successfully started in the SF cluster

References



Premier Support for Developers provides strategic technology guidance, critical support coverage, and a range of essential services to help teams optimize development lifecycles and improve software quality.  Contact your Application Development Manager (ADM) or email us to learn more about what we can do for you.

Author

Developer Support
Cloud Solution Architects

Microsoft Developer Support helps software developers rapidly build and deploy quality applications for Microsoft platforms.

0 comments

Discussion are closed.