Bibliothèques Azure Service Fabric pour .NET

Vue d’ensemble

Azure Service Fabric est une plateforme de systèmes distribués qui facilite le packaging, le déploiement et la gestion de conteneurs et de microservices évolutifs et fiables. Pour plus d’informations, consultez la documentation relative à Azure Service Fabric.

Bibliothèque cliente

Utilisez la bibliothèque cliente de Service Fabric pour interagir avec un cluster Service Fabric existant. La bibliothèque contient trois catégories d’API :

  • Les API client sont utilisées pour gérer, mettre à l’échelle et recycler le cluster, ainsi que pour déployer des packages d’application.
  • Les API runtime sont utilisées pour l’application en cours d’exécution afin d’interagir avec son cluster hôte.
  • Les API communes contiennent des types utilisés dans les API client et runtime.

Installez le package NuGet directement à partir de la Console du Gestionnaire de package Visual Studio ou avec la CLI .NET Core.

Gestionnaire de package Visual Studio

Install-Package Microsoft.ServiceFabric
dotnet add package Microsoft.ServiceFabric

Exemples de code

L’exemple suivant utilise les API client de Service Fabric pour copier un package d’application dans le magasin d’images, provisionne le type d’application, et crée une instance d’application.

/* Include these dependencies
using System.Fabric;
using System.Fabric.Description;
*/

// Connect to the cluster.
FabricClient fabricClient = new FabricClient(clusterConnection);

// Copy the application package to a location in the image store
fabricClient.ApplicationManager.CopyApplicationPackage(imageStoreConnectionString, packagePath, packagePathInImageStore);

// Provision the application.
fabricClient.ApplicationManager.ProvisionApplicationAsync(packagePathInImageStore).Wait();

//  Create the application instance.
ApplicationDescription appDesc = new ApplicationDescription(new Uri(appName), appType, appVersion);
fabricClient.ApplicationManager.CreateApplicationAsync(appDesc).Wait();

Cet exemple utilise les API runtime et communes de Service Fabric à partir d’une application hôte pour mettre à jour une collection fiable lors de l’exécution.

using System.Fabric;
using Microsoft.ServiceFabric.Data.Collections;
using Microsoft.ServiceFabric.Services.Communication.Runtime;
using Microsoft.ServiceFabric.Services.Runtime;

/// <summary>
/// This is the main entry point for your service replica.
/// This method executes when this replica of your service becomes primary and has write status.
/// </summary>
/// <param name="cancellationToken">Canceled when Service Fabric needs to shut down this service replica.</param>
protected override async Task RunAsync(CancellationToken cancellationToken)
{
    var myDictionary = await this.StateManager.GetOrAddAsync<IReliableDictionary<string, long>>("myDictionary");
    while (true)
    {
        cancellationToken.ThrowIfCancellationRequested();
        using (var tx = this.StateManager.CreateTransaction())
        {
            var result = await myDictionary.TryGetValueAsync(tx, "Counter");
            await myDictionary.AddOrUpdateAsync(tx, "Counter", 0, (key, value) => ++value);

            // If an exception is thrown before calling CommitAsync, the transaction aborts, all changes are
            // discarded, and nothing is saved to the secondary replicas.
            await tx.CommitAsync();
        }
        await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
    }
}

Bibliothèque de gestion

La bibliothèque de gestion est utilisée pour la création, la mise à jour et la suppression des clusters Service Fabric.

Installez le package NuGet directement à partir de la Console du Gestionnaire de package Visual Studio ou avec la CLI .NET Core.

Gestionnaire de package Visual Studio

Install-Package Microsoft.Azure.Management.ServiceFabric
dotnet add package Microsoft.Azure.Management.ServiceFabric

Exemples