Partilhar via


Bibliotecas do Azure Service Fabric para .NET

Visão geral

O Azure Service Fabric é uma plataforma de sistemas distribuídos que facilita o empacotamento, implantação e gerenciamento de microsserviços e contêineres escalonáveis e confiáveis. Para obter mais informações, consulte a Documentação do Azure Service Fabric.

Biblioteca do cliente

Use a biblioteca de cliente do Service Fabric para interagir com um cluster Service Fabric existente. A biblioteca contém três categorias de APIs:

  • As APIs do cliente são usadas para gerenciar, dimensionar e reciclar o cluster, bem como implantar pacotes de aplicativos.
  • As APIs de execução são usadas para o aplicativo em execução interagir com seu cluster de hospedagem.
  • As APIs comuns contêm os tipos usados nas APIs do cliente e de execução.

Instale o pacote NuGet diretamente do console do Gerenciador de Pacotes do Visual Studio ou com a CLI do .NET Core.

Gerenciador de Pacotes do Visual Studio

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

Exemplos de código

O exemplo a seguir usa as APIs do cliente do Service Fabric para copiar um pacote de aplicativos para o repositório de imagens, provisionar o tipo de aplicativo e criar uma instância dele.

/* 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 exemplo usa as APIs de execução e comuns do Service Fabric de dentro de um aplicativo hospedado para atualizar uma Coleção Confiável durante a execução.

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 Gerenciamento

A biblioteca de gerenciamento é usada para criar, atualizar e excluir os clusters Service Fabric.

Instale o pacote NuGet diretamente do console do Gerenciador de Pacotes do Visual Studio ou com a CLI do .NET Core.

Gerenciador de Pacotes do Visual Studio

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

Exemplos