適用於 .NET 的 Azure Service Fabric 程式庫Azure Service Fabric libraries for .NET

概觀Overview

Azure Service Fabric 是一個分散式系統平台,可讓您輕鬆封裝、部署及管理可調整和可信賴的微服務與容器。Azure Service Fabric is a distributed systems platform that makes it easy to package, deploy, and manage scalable and reliable microservices and containers. 如需詳細資訊,請參閱 Azure Service Fabric 文件For more information, see the Azure Service Fabric Documentation.

用戶端程式庫Client library

使用 Service Fabric 用戶端程式庫與現有的 Service Fabric 叢集互動。Use the Service Fabric client library to interact with an existing Service Fabric cluster. 程式庫包含 3 種 API 類別:The library contains three categories of APIs:

  • 用戶端 API 用於管理、調整及回收叢集,以及部署應用程式套件。Client APIs are used to manage, scale, and recycle the cluster, as well as deploy application packages.
  • 執行階段 API 用於讓應用程式與其主機伺服器互動。Runtime APIs are used for the running application to interact with its hosting cluster.
  • 一般 API 包含在用戶端執行階段 API 中使用的類型。Common APIs contain types used in both client and runtime APIs.

直接從 Visual Studio 套件管理員主控台安裝 NuGet 套件,或使用 .NET Core CLIInstall the NuGet package directly from the Visual Studio Package Manager console or with the .NET Core CLI.

Visual Studio 套件管理員Visual Studio Package Manager

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

程式碼範例Code Examples

下列範例指令碼會使用 Service Fabric 用戶端 API 將應用程式套件複製到映像存放區、佈建應用程式類型,並建立應用程式執行個體。The following example uses the Service Fabric client APIs to copy an application package to the image store, provisions the application type, and create an application instance.

/* 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,在執行階段更新 Reliable CollectionThis example uses the Service Fabric runtime and common APIs from within a hosted application to update a Reliable Collection at runtime.

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

管理程式庫Management Library

管理程式庫用來建立、更新及刪除 Service Fabric 叢集。The management library is used to create, update, and delete Service Fabric clusters.

直接從 Visual Studio 套件管理員主控台安裝 NuGet 套件,或使用 .NET Core CLIInstall the NuGet package directly from the Visual Studio Package Manager console or with the .NET Core CLI.

Visual Studio 套件管理員Visual Studio Package Manager

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

範例Samples