2016-03-08 107 views
3

我正在尝试获取给定用户的lync状态。我的代码将使用64位环境中的UCMA 4.0与lync server 2010进行通信。等待通知异步调用

这里是我的代码,等待异步调用来获得lync状态。

private async void getNotifications(UserEndpoint endpoint, string useridSIP) 
{ 
    _userEndpoint.PresenceServices.BeginPresenceQuery(
     new[] { useridSIP }, 
     new[] { "state" }, 
     null, 
     (ar) => { 
      Task<List<RemotePresentityNotification>> notificationFetch = _userEndpoint.PresenceServices.EndPresenceQuery(ar).ToList<RemotePresentityNotification>(); 
      List<RemotePresentityNotification> result = await notificationFetch; 
      result.ForEach(x => { 
       LyncUser user = new LyncUser(); 
       if (x.AggregatedPresenceState != null) 
       { 
        user.Presence = x.AggregatedPresenceState.Availability.ToString(); 
       } 
       else 
       { 
        user.Presence = "Unknown"; 
       } 
       user.UserName = x.PresentityUri.ToString(); 
       usersWithStatus.Add(user); 
      }); 
     }, 
     null); 
} 

我不知道如何等到List<RemotePresentityNotification>结果返回

Task<List<RemotePresentityNotification>> notificationFetch = _userEndpoint.PresenceServices.EndPresenceQuery(ar).ToList<RemotePresentityNotification>(); 
List<RemotePresentityNotification> result = await notificationFetch; 

整个源代码。

using Microsoft.Rtc.Collaboration; 
using Microsoft.Rtc.Collaboration.Presence; 
using Oobe.Bobs.Lync.Models; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading; 
using System.Threading.Tasks; 
using System.Web; 

namespace xxxx.xxxx.xxxx 
{ 
    public class OneTimePresence 
    { 
     private UCMASampleHelper _helper; 
     private UserEndpoint _userEndpoint; 
     public List<LyncUser> usersWithStatus = new List<LyncUser>(); 
     public LyncPresenceChecker _checker { get; set; } 
     public OneTimePresence(string useridSIP, LyncPresenceChecker checker) 
     { 
      _checker = checker; 
      _helper = new UCMASampleHelper(); 
      string endpoint = String.Format("OneTime Presence query for {0}", useridSIP); 
      _userEndpoint = _helper.CreateEstablishedUserEndpoint(endpoint); 
      getNotifications(_userEndpoint, useridSIP); 
      _helper.ShutdownPlatform(); 

     } 
     protected void EndgetNotification(object sender, RemotePresentitiesNotificationEventArgs e) 
     { 
      e.Notifications.ToList<RemotePresentityNotification>().ForEach(x => 
      { 
       LyncUser user = new LyncUser(); 
       if (x.AggregatedPresenceState != null) 
       { 
        user.Presence = x.AggregatedPresenceState.Availability.ToString(); 
       } 
       else 
       { 
        user.Presence = "Unknown"; 
       } 
       user.UserName = x.PresentityUri.ToString(); 
       usersWithStatus.Add(user); 
      }); 
      _checker.Clients.All.updateLyncUserPresence(usersWithStatus); 

     } 

     private void getNotifications(UserEndpoint endpoint, string useridSIP) 
     { 
      _userEndpoint.PresenceServices.BeginPresenceQuery(
       new[] { useridSIP }, 
       new[] { "state" }, 
       EndgetNotification, 
       (ar) => { 
        ar.AsyncWaitHandle.WaitOne(); 
        List<RemotePresentityNotification> result = _userEndpoint.PresenceServices.EndPresenceQuery(ar).ToList<RemotePresentityNotification>(); 

        result.ForEach(x => 
        { 
         LyncUser user = new LyncUser(); 
         if (x.AggregatedPresenceState != null) 
         { 
          user.Presence = x.AggregatedPresenceState.Availability.ToString(); 
         } 
         else 
         { 
          user.Presence = "Unknown"; 
         } 
         user.UserName = x.PresentityUri.ToString(); 
         usersWithStatus.Add(user); 
        }); 
       }, 
       null); 
      if (usersWithStatus.Count > 0) 
      { 
       _checker.Clients.All.updateLyncUserPresence(usersWithStatus); 
      } 

     } 


    } 
} 
+0

请勿使用async void。请参阅[本](https://msdn.microsoft.com/en-us/magazine/jj991977.aspx?f=255&MSPPError=-2147217396)文章以获取完整说明。将getNotifications的方法签名更改为async Task。你为什么要等待结果? –

+0

lync服务器需要时间返回状态,因此匿名方法被异步调用 –

回答

1

我相信你正在寻找Task.Factory.FromAsync方法。此方法是BeginEndasync模式 - detailed here的包装。比如你想这样做,而不是:

private async Task<List<RemotePresentityNotification>> GetNotifications(UserEndpoint endpoint, string useridSIP) 
{ 
    var task = Task.Factory.FromAsync(
     _userEndpoint.PresenceServices.BeginPresenceQuery, 
     _userEndpoint.PresenceServices.EndPresenceQuery, 
     new[] { useridSIP }, 
     new[] { "state" }); 

    var results = await task; 
    return results.ToList(); 
} 
  1. 避免async void详细here
  2. 使用async只要有相应的await

有了这个地方,你可以据此await它并按照您认为合适的方式处理它,如下所示:

private async Task SomeCaller(UserEndpoint endpoint, string useridSIP) 
{ 
    var list = await GetNotifications(endpoint, useridSIP); 
    // ... do stuff with it 
} 

更新1

考虑确保PresenceServices实际上可通过检查State详细的here

只要端点的状态属性设置为“已建立”,所有状态服务都可用。

此外,this可能有助于看。

+0

1.您说要省略'async',但是不要忽略它。 2.这是行不通的,['EndPresenceQuery'](https://msdn.microsoft.com/en-us/library/office/hh366226.aspx)返回'IEnumerable ',所以得到'List ',你需要在某处调用'ToList()'(就像问题中的代码一样)。 – svick

+0

给我所有的来源,我可以更好地帮助你 –

+0

我不是OP,我没有比你更多的代码。 – svick