azure - 最简单的 Service Fabric 可靠集合(字典)示例

标签 azure azure-service-fabric

我正在尝试实现Azure Service Fabric Reliable Collection的最简单用法--- Hello World 示例,可以说是 --- I​Reliable​Dictionary

this link给出了一个例子。然而这个例子需要一个 StateManager 对象,我不确定如何创建它,甚至不确定它是什么。这似乎是Actor是需要的,我现在希望避免使用 Actor。

谁能举个例子吗?

提前非常感谢

最佳答案

没关系,我找到了答案。

关键的一点是,无法在无状态服务中创建可靠的集合,如果我们想要使用有状态服务,我们就必须创建一个有状态服务 em>可靠的收藏。

Here我找到了可靠字典的实现示例。接下来我粘贴代码,该代码应位于 MyStatefulService.cs 类中:

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

        ServiceEventSource.Current.ServiceMessage(this, "Current Counter Value: {0}",
            result.HasValue ? result.Value.ToString() : "Value does not exist.");

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

关于azure - 最简单的 Service Fabric 可靠集合(字典)示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44681498/

相关文章:

azure - 1 个 VM 连接到 2 个 VNET?

c# - 使用 WS-MetadataExchange 为外部客户端和 WcfTestClient 公开 Service Fabric 中的 WCF/TCP 终结点

azure - 在 Azure SQL 数据仓库中创建外部表

azure - 如何创建 Azure Kusto 查询以仅在应用程序见解上按客户端操作系统名称(操作系统版本已删除)进行分组?

azure - 如何使用 Java 验证 Azure AD JWT token ?

Azure Service Fabric Actor 提醒更新?

azure-service-fabric - 服务 : use IsCancellationRequested or throw exception 中 RunAsync 方法的 while 循环中什么是首选

c# - 访问 Azure Service Fabric 有状态服务状态

api - 运行集成测试的最佳实践是什么?

azure - 微软!德语! Azure Cloud - 让 oauth2-proxy 工作