1
[FunctionName("SetDurable")]
public static async Task<HttpResponseMessage> SetDurable(
        [HttpTrigger(AuthorizationLevel.Function, "get", Route = "SetDurable/{durable}")] HttpRequestMessage req,
        [DurableClient] IDurableEntityClient client,
        string durable)
        {
           var entityId = new EntityId(DurableEntitiesNames.BirSessionIdentificatorEntity, DurableEntitiesNames.BirSessionIdentificatorKey);
           await client.SignalEntityAsync<IBirSessionIdentificator>(entityId, identificator => identificator.Set(durable) );
           //await Task.Delay(2000);
           EntityStateResponse<JObject> stateResponse = await client.ReadEntityStateAsync<JObject>(entityId);
           var setResult = stateResponse.EntityState.ToObject<BirSessionIdentificatorResult>().Sid;
           return new HttpResponseMessage(HttpStatusCode.OK){Content = new StringContent($"{setResult}")};
    }
    

In Azure Functions v3 when i try to set the value of durable entity and then immediately try to read this value, it returns old value instead of the new value. But when i uncomment the Task.Delay and make it wait 2 seconds after setting the value of durable entity i get correct value while trying to read it.

Is there any way to await the completion of durable entity value setting operation?

4

1 回答 1

1

有没有办法等待持久实体值设置操作完成?

不,至少在使用SignalEntityAsync. 请参阅文档

重要的是要了解从客户端发送的“信号”只是简单地排队,以便稍后异步处理。特别是,SignalEntityAsync通常在实体开始操作之前就返回,并且不可能取回返回值或观察异常。如果需要更强的保证(例如工作流),则应使用编排器功能,它可以等待实体操作完成,并且可以处理返回值并观察异常。

粗体部分是您的出路:您可以使用编排中的代理来访问持久实体。这样你就可以等待操作了。一个例子:

[FunctionName("DeleteCounter")]
public static async Task<HttpResponseMessage> DeleteCounter(
    [HttpTrigger(AuthorizationLevel.Function, "delete", Route = "Counter/{entityKey}")] HttpRequestMessage req,
    [DurableClient] IDurableEntityClient client,
    string entityKey)
{
    var entityId = new EntityId("Counter", entityKey);
    await client.SignalEntityAsync<ICounter>(entityId, proxy => proxy.Delete());    
    return req.CreateResponse(HttpStatusCode.Accepted);
}

在这里,我们等待实体上 Delete 方法的调用。

于 2020-10-21T19:10:08.050 回答