ServicePartitionResolver.ResolveAsync永远挂起

时间:2019-01-24 07:42:38

标签: c# azure service-fabric-stateless

当我使用以下代码时,方法ResolveAsync永远挂起。

如何解析分区?

ServicePartitionKey.Singleton更改为new ServicePartitionKey()不能解决问题。

internal sealed class CryptoPriceService : StatelessService
{
[...]
  protected override Task RunAsync(CancellationToken cancellationToken)
       {
           return Task.Run(() =>
           {
               var resolver = ServicePartitionResolver.GetDefault();
               var partition = resolver.ResolveAsync(new Uri("fabric:/CryptoPriceWebSite/OtherService"),
                   ServicePartitionKey.Singleton, TimeSpan.FromSeconds(15), TimeSpan.FromSeconds(15),
                   cancellationToken).Result; // Hang on forever
               var addressJson = JObject.Parse(partition.GetEndpoint().Address);
               var address = addressJson["Endpoints"][Constantes.BusEndpoint].ToString();

 [...]
               Thread.Sleep(Timeout.Infinite);
           });
       }
}

1 个答案:

答案 0 :(得分:0)

我知道这个相当老的问题,但是为了不使该方法挂起,您应该传递具有所需超时的取消令牌。 例如:

CancellationTokenSource cancellationToken = new CancellationTokenSource(TimeSpan.FromMinutes(10));
resolver.ResolveAsync(new Uri("fabric:/CryptoPriceWebSite/OtherService"),
               ServicePartitionKey.Singleton, TimeSpan.FromSeconds(15), TimeSpan.FromSeconds(15),
               cancellationToken.Token).Result;