Dela via


Räkna upp Service Fabric Reliable Actors

Reliable Actors-tjänsten gör att en klient kan räkna upp metadata om de aktörer som tjänsten är värd för. Eftersom aktörstjänsten är en partitionerad tillståndskänslig tjänst utförs uppräkning per partition. Eftersom varje partition kan innehålla många aktörer returneras uppräkningen som en uppsättning växlingsresultat. Sidorna loopas över tills alla sidor har lästs. I följande exempel visas hur du skapar en lista över alla aktiva aktörer i en partition av en aktörstjänst:

IActorService actorServiceProxy = ActorServiceProxy.Create(
    new Uri("fabric:/MyApp/MyService"), partitionKey);

ContinuationToken continuationToken = null;
List<ActorInformation> activeActors = new List<ActorInformation>();

do
{
    PagedResult<ActorInformation> page = await actorServiceProxy.GetActorsAsync(continuationToken, cancellationToken);

    activeActors.AddRange(page.Items.Where(x => x.IsActive));

    continuationToken = page.ContinuationToken;
}
while (continuationToken != null);
ActorService actorServiceProxy = ActorServiceProxy.create(
    new URI("fabric:/MyApp/MyService"), partitionKey);

ContinuationToken continuationToken = null;
List<ActorInformation> activeActors = new ArrayList<ActorInformation>();

do
{
    PagedResult<ActorInformation> page = actorServiceProxy.getActorsAsync(continuationToken);

    while(ActorInformation x: page.getItems())
    {
         if(x.isActive()){
              activeActors.add(x);
         }
    }

    continuationToken = page.getContinuationToken();
}
while (continuationToken != null);

Koden ovan hämtar alla aktörer i en viss partition, men ibland uppstår behovet av att fråga ID:t för alla aktörer (aktiva eller inaktiva) över varje partition. Detta bör göras av undantag eftersom det är en ganska tung uppgift.

I följande exempel visas hur du kör frågor mot partitionerna i tjänsten och itererar genom var och en i kombination med exemplet ovan för att skapa en lista över alla aktiva och inaktiva aktörer i tjänsten i Service Fabric-programmet:


var serviceName = new Uri("fabric:/MyApp/MyService");

//As the FabricClient is expensive to create, it should be shared as much as possible
FabricClient fabricClient = new();

//List each of the service's partitions
ServicePartitionList partitions = await fabricClient.QueryManager.GetPartitionListAsync(serviceName);

List<Guid> actorIds = new();

foreach(var partition in partitions)
{
    //Retrieve the partition information
    Int64RangePartitionInformation partitionInformation = (Int64RangePartitionInformation)partition.PartitionInformation; //Actors are restricted to the uniform Int64 scheme per https://learn.microsoft.com/azure/service-fabric/service-fabric-reliable-actors-introduction#distribution-and-failover
    IActorService actorServiceProxy = ActorServiceProxy.Create(serviceName, partitionInformation.LowKey);
    
    ContinuationToken? continuationToken = null;
    
    do 
    {
        var page = await actorServiceProxy.GetActorsAsync(continuationToken, cancellationToken);
        actorIds.AddRange(page.Items.Select(actor => actor.ActorId.GetGuidId());
        continuationToken = page.ContinuationToken;
    } while (continuationToken != null);
}

return actorIds;

Nästa steg