2016-10-10 1 views
0

현재 API에서 비동기 메서드로 동기화를 제공해야합니다. 아래 코드를 찾으십시오. 유일한 문제는 백엔드에 동기화 방법이 없다는 것입니다. Azure.NotificationHub 클라이언트를 사용합니다. 해당 클라이언트는 * 비동기 메소드 만 있습니다. 내 방식이 합리적입니까? 어떤 조언을Microsoft.Azure.NotificationHub - 동기화 메서드의 비동기 호출

public PushHubNotificationResult SendPushMessage(string userId, string message) 
    { 
     PushHubNotificationResult result = new PushHubNotificationResult(); 
     try 
     { 
      result = SendPushMessageAsync(userId, message).GetAwaiter().GetResult(); 

     } catch (Exception ex) 
     { 
      result.Status = PushHubNotificationResultType.Error; 
      result.Error = ex.Message; 
      result.Exception = ex; 
     } 

     return result; 
    } 

    public async Task<PushHubNotificationResult> SendPushMessageAsync(string userId, string message) 
    { 
     PushHubNotificationResult result = new PushHubNotificationResult(); 

     // EnableTestSend see: https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-push-notification-fixer/#self-diagnose-tips 

     // Create a new Notification Hub client. 
     Microsoft.Azure.NotificationHubs.NotificationHubClient hub = 
      Microsoft.Azure.NotificationHubs.NotificationHubClient.CreateClientFromConnectionString(NotificationHub, NotificationHubName); 

     // Sending the message so that all template registrations that contain "messageParam" 
     // will receive the notifications. This includes APNS, GCM, WNS, and MPNS template registrations. 
     Dictionary<string, string> templateParams = new Dictionary<string, string>(); 

     templateParams["messageParam"] = message; 

     string userTag = "_UserId:" + userId; // That line sets the IMEI or SerialNo (WLAN only device) == userId to which the push message is sent 

     try 
     { 
      // Send the push notification and log the results. 
      NotificationOutcome outcome = await hub.SendTemplateNotificationAsync(templateParams, userTag); 

      result.Status = PushHubNotificationResultType.Success; 

      foreach (RegistrationResult hubResult in outcome.Results) 
      { 
       result.PushNotificationHub = hubResult.ApplicationPlatform; 
       result.RegistrationId = hubResult.RegistrationId; 
       result.Outcome = hubResult.Outcome; 
      } 

     } 
     catch (System.Exception ex) 
     { 
      result.Status = PushHubNotificationResultType.Error; 
      result.Error = ex.Message; 
      result.Exception = ex; 
     } 

     return result; 
    } 

덕분에, 에릭

답변

1

당신이 동기 대비 비동기를 사용하려는 경우, 그것은 그렇지 않으면 교착 상태를 얻을 가능성이 매우 높다, 당신이 당신의 async 코드에서 ConfigureAwait(false)를 사용하는 것이 매우 중요합니다 .

NotificationOutcome outcome = 
    await hub.SendTemplateNotificationAsync(templateParams, userTag).ConfigureAwait(false); 

비동기 방식은 이미 PushHubNotificationResultType.Error에 대한 예외를 변환, 왜 동기화 버전이 너무 그것을합니까?

+0

감사합니다. 네 말이 맞아. 그것은 제거되어야합니다. –

관련 문제