Azure service fabric 如何使用CancellationToken调用Azure服务结构远程代理?

Azure service fabric 如何使用CancellationToken调用Azure服务结构远程代理?,azure-service-fabric,service-fabric-remoting,Azure Service Fabric,Service Fabric Remoting,根据文档示例,代理的创建方式如下: IMyService helloWorldClient = ServiceProxy.Create<IMyService>(new Uri("fabric:/MyApplication/MyHelloWorldService")); string message = await helloWorldClient.HelloWorldAsync(); IMyService helloWorldClient=ServiceProxy.Create

根据文档示例,代理的创建方式如下:

IMyService helloWorldClient = ServiceProxy.Create<IMyService>(new 
Uri("fabric:/MyApplication/MyHelloWorldService"));

string message = await helloWorldClient.HelloWorldAsync();
IMyService helloWorldClient=ServiceProxy.Create(新建)
Uri(“结构:/MyApplication/MyHelloWorldService”);
字符串消息=等待helloWorldClient.HelloWorldAsync();

但是如果我需要限制最大响应时间,我通常会创建CancellationToken并将其传递给呼叫。有没有办法将令牌传递给代理,以便它取消等待远程服务的结果?

据我所知,将取消令牌传递给调用是不可能的,因为它只是一个代理,它只接受您在接口中描述的方法的参数

要完成所需操作,请在特定时间后取消操作,您必须配置并将OperationTimeout设置为所需的超时。默认值为5分钟

您还可以通过service settings.xml中的TransportSettings进行此设置

以下链接将显示如何设置FabricTransportRemotingListenerSettings或TransportSettings配置的两个示例。

文档未显示,但必须设置的参数为:OperationTimeout


演员也可以这样做,请参见,注意演员的某些设置是不同的。

您可以在V2堆栈中执行此操作,我没有尝试V1堆栈,但它也可能在那里工作。将CancellationToken类型的参数添加到方法签名:

void HelloWorldAsync(CancellationToken cancellationToken);
然后,该令牌将通过代理传递给ServicePartitionClient.InvokeWithRetryAsync方法

生成的代理方法如下所示:

Task<string> IMyService.HelloWorldAsync(CancellationToken cancellationToken)
{
    IServiceRemotingRequestMessageBody serviceRemotingRequestMessageBody = base.CreateRequestMessageBodyV2("MyNamespace.IMyService", "HelloWorldAsync", 1);
    Task<IServiceRemotingResponseMessageBody> task = base.InvokeAsyncV2(
    1, -1, "HelloWorldAsync", serviceRemotingRequestMessageBody,
    cancellationToken);
    return base.ContinueWithResultV2<ServiceInfoResponse>(task);
}

对于那些仍然对正确地完成这条线索所要求的事情感兴趣的人,下面的链接是答案吗

内容:

取消iSeries设备/IActor的令牌支持

可靠服务和可靠参与者方法现在支持可通过ActorProxy和ServiceProxy进行远程的取消令牌,允许您实现协作取消。想要取消长时间运行的服务或参与者方法的客户端可以向取消令牌发送信号,并且取消意图将传播到参与者/服务方法。然后,该方法可以通过查看其取消令牌参数的状态来确定何时停止执行

例如,具有可能长期运行的方法的参与者的契约可以建模,如下所示:

    public interface IPrimeNumberActorInterface : IActor
    {

        Task<ulong> FindNextPrimeNumberAsync
            (ulong previous, CancellationToken cancellationToken);

    }
公共接口IPrimeNumberActorInterface:IActor
{
任务FindNextPrimeNumberAsync
(ulong previous,CancellationToken CancellationToken);
}
希望取消方法执行的客户机代码可以通过取消取消令牌来传达其意图

[assembly: CodeBuilder(EnableDebugging = true)]
    public interface IPrimeNumberActorInterface : IActor
    {

        Task<ulong> FindNextPrimeNumberAsync
            (ulong previous, CancellationToken cancellationToken);

    }