列舉 Service Fabric Reliable Actors
Reliable Actors 服務允許用戶端列舉服務所裝載之動作項目的相關中繼資料。 因為動作項目服務是已資料分割的具狀態服務,所以會針對每個分割區執行列舉。 由於每個分割區可能包含許多動作項目,因此,列舉會以一組分頁式結果形式傳回。 頁面會以迴圈方式讀取,直到讀取所有頁面為止。 下列範例顯示如何在動作項目服務的其中一個分割區中,建立所有作用中動作項目的清單:
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);
即使上述的程式碼會結取指定分割區中的所有動作項目,但有時您可能必須查詢每個分割區中所有動作項目 (作用中或非作用中) 的識別碼。 這應以例外狀況來完成,因為是相當繁重的工作。
下列範例示範如何查詢服務的分割區並反覆查看每個分割區和上述範例,以產生 Service Fabric 的服務中所有作用中和非作用中動作項目清單:
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;