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