【问题标题】:How to call Azure service fabric remoting proxy with CancellationToken?如何使用 CancellationToken 调用 Azure 服务结构远程代理?
【发布时间】:2018-12-21 21:40:36
【问题描述】:

根据文档示例,代理是这样创建的:

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

string message = await helloWorldClient.HelloWorldAsync();

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

【问题讨论】:

    标签: azure-service-fabric service-fabric-remoting


    【解决方案1】:

    您可以在 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);
    }
    

    如果没有 CancellationToken 参数,代理方法如下所示:

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

    如果要检查生成的代理程序集,请在 EntryPoint 程序集或定义了服务接口的程序集中使用以下属性:

    [assembly: CodeBuilder(EnableDebugging = true)]
    

    【讨论】:

      【解决方案2】:

      对于仍然对正确执行此线程所要求的内容感兴趣的人,下面的链接可以是答案吗?

      https://blogs.msdn.microsoft.com/azureservicefabric/2016/02/23/service-fabric-sdk-v1-5-175-and-the-adoption-of-virtual-machine-scale-sets/

      内容:

      对 IService/IActor 的 CancellationToken 支持

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

      例如,一个参与者的合约可能有一个可能长时间运行的方法,可以如下建模:

          public interface IPrimeNumberActorInterface : IActor
          {
      
              Task<ulong> FindNextPrimeNumberAsync
                  (ulong previous, CancellationToken cancellationToken);
      
          }
      

      希望取消方法执行的客户端代码可以通过取消取消令牌来传达其意图。

      【讨论】:

        【解决方案3】:

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

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

        您也可以通过服务 settings.xml 中的 TransportSettings 进行此设置。

        以下链接将显示如何设置 FabricTransportRemotingListenerSettings 或 TransportSettings 配置的两个示例。 https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-reliable-services-secure-communication

        文档没有显示,但是你要设置的参数是:OperationTimeout

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

        【讨论】:

          猜你喜欢
          • 2017-11-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多