gpt4 book ai didi

c# - 等待持久实体值发生变化

转载 作者:行者123 更新时间:2023-12-02 23:39:31 26 4
gpt4 key购买 nike

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

在 Azure Functions v3 中,当我尝试设置持久实体的值,然后立即尝试读取该值时,它返回旧值而不是新值。但是,当我取消注释 Task.Delay 并使其在设置持久实体的值后等待 2 秒时,我在尝试读取它时获得了正确的值。

有什么办法可以等待持久实体值设置操作完成吗?

最佳答案

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

不,至少在使用 SignalEntityAsync 时不会。请参阅the docs :

It's important to understand that the "signals" sent from the client are simply enqueued, to be processed asynchronously at a later time. In particular, the SignalEntityAsync usually returns before the entity even starts the operation, and it is not possible to get back the return value or observe exceptions. If stronger guarantees are required (e.g. for workflows), orchestrator functions should be used, which can wait for entity operations to complete, and can process return values and observe exceptions.

粗体部分是你的出路:你可以使用proxy从编排内部访问持久实体。这样就可以等待手术了。一个例子:

[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方法的调用。

关于c# - 等待持久实体值发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64427982/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com