您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

用于 .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 CLI 文档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. 该库包含三个类别的 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 和运行时 API 中使用的类型。Common APIs contain types used in both client and runtime APIs.

直接从 Visual Studio 包管理器控制台或使用 .NET Core CLI 安装 NuGet 包Install 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 和公用 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 包管理器控制台或使用 .NET Core CLI 安装 NuGet 包Install 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