Azure Service Fabric-Bibliotheken für .NET

Übersicht

Azure Service Fabric ist eine Plattform für verteilte Systeme, die das Packen, Bereitstellen und Verwalten skalierbarer und zuverlässiger Microservices und Container vereinfacht. Weitere Informationen finden Sie in der Dokumentation zu Azure Service Fabric.

Clientbibliothek

Verwenden Sie für die Interaktion mit einem vorhandenen Service Fabric-Cluster die Service Fabric-Clientbibliothek. Die Bibliothek enthält drei Kategorien von APIs:

  • Client-APIs werden zum Verwalten, Skalieren und Wiederverwenden des Clusters sowie zum Bereitstellen von Anwendungspaketen verwendet.
  • Runtime-APIs werden für die Interaktion der ausgeführten Anwendung mit ihrem Hostcluster verwendet.
  • Allgemeine APIs enthalten Typen, die sowohl in Client- als auch in Runtime-APIs verwendet werden.

Installieren Sie das NuGet-Paket direkt über die Paket-Manager-Konsole in Visual Studio oder mit der .NET Core CLI.

Visual Studio-Paket-Manager

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

Codebeispiele

Im folgenden Beispiel wird mithilfe der Client-APIs von Service Fabric ein Anwendungspaket in den Imagespeicher kopiert, der Anwendungstyp bereitgestellt und eine Instanz der Anwendung erstellt.

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

In diesem Beispiel werden die Runtime-APIs und die allgemeinen APIs von Service Fabric in einer gehosteten Anwendung verwendet, um zur Laufzeit eine Reliable Collection zu aktualisieren.

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

Verwaltungsbibliothek

Die Verwaltungsbibliothek wird zum Erstellen, Aktualisieren und Löschen von Service Fabric-Clustern verwendet.

Installieren Sie das NuGet-Paket direkt über die Paket-Manager-Konsole in Visual Studio oder mit der .NET Core CLI.

Visual Studio-Paket-Manager

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

Beispiele