Compartir a través de


Bibliotecas de Azure Service Fabric para .NET

Información general

Azure Service Fabric es una plataforma de sistemas distribuidos que facilita el empaquetado, la implementación y la administración de microservicios y contenedores escalables y confiables. Para más información, consulte la documentación de Azure Service Fabric.

Biblioteca de cliente

Utilice la biblioteca de cliente de Service Fabric para interactuar con un clúster de Service Fabric existente. La biblioteca contiene tres categorías de API:

  • Cliente Las API se utilizan para administrar, escalar y reciclar el clúster, así como para implementar paquetes de aplicaciones.
  • En tiempo de ejecución Las API se utilizan para que la aplicación en ejecución pueda interactuar con el clúster que la aloja.
  • Común Las API contienen tipos usados tanto en API de cliente como en tiempo de ejecución.

Instale el paquete NuGet directamente desde la Consola del Administración de paquetes de Visual Studio o con la CLI de .NET Core.

Administrador de paquetes de Visual Studio

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

Ejemplos de código

En el ejemplo siguiente se utilizan las API de cliente de Service Fabric para copiar un paquete de aplicación en el almacén de imágenes, aprovisionar el tipo de aplicación y crear una instancia de la aplicación.

/* 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();

Este ejemplo utiliza las API en tiempo de ejecución y común de Service Fabric desde una aplicación hospedada para actualizar una Colección confiable en tiempo de ejecución.

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);
    }
}

Biblioteca de administración

La biblioteca de administración se utiliza para crear, actualizar y eliminar clústeres de Service Fabric.

Instale el paquete NuGet directamente desde la Consola del Administración de paquetes de Visual Studio o con la CLI de .NET Core.

Administrador de paquetes de Visual Studio

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

Ejemplos