.NET 用 Azure Service Fabric ライブラリ

概要

Azure Service Fabric は、スケーラブルで信頼性に優れたマイクロサービスとコンテナーのパッケージ化とデプロイ、管理を簡単に行うことができる分散システム プラットフォームです。 詳細については、「Azure Service Fabric のドキュメント」を参照してください。

クライアント ライブラリ

既存の Service Fabric クラスターと対話するには、Service Fabric クライアント ライブラリを使用します。 このライブラリには、3 つのカテゴリの API が含まれています。

  • クライアント API は、クラスターの管理、スケーリング、リサイクルに加え、アプリケーション パッケージのデプロイに使用されます。
  • ランタイム API は、実行中のアプリケーションが、ホストしているクラスターと対話する場合に使用されます。
  • 一般的な API には、クライアント API とランタイム API の両方で使用される種類が含まれています。

NuGet パッケージを Visual Studio パッケージ マネージャー コンソールから直接インストールするか、.NET Core CLI を使ってインストールします。

Visual Studio パッケージ マネージャー

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

コード例

次の例では、Service Fabric のクライアント API を使用してアプリケーション パッケージをイメージ ストアにコピーし、アプリケーションの種類をプロビジョニングして、アプリケーション インスタンスを作成します。

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

この例では、ホストされるアプリケーション内から Service Fabric のランタイム API と一般的な API を使用して、実行時に Reliable Collection を更新します。

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

管理ライブラリ

管理ライブラリは、Service Fabric クラスターの作成、更新、削除に使用されます。

NuGet パッケージを Visual Studio パッケージ マネージャー コンソールから直接インストールするか、.NET Core CLI を使ってインストールします。

Visual Studio パッケージ マネージャー

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

サンプル