我正在尝试获取给定用户的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);
}
}
}
}
请勿使用async void。请参阅[本](https://msdn.microsoft.com/en-us/magazine/jj991977.aspx?f=255&MSPPError=-2147217396)文章以获取完整说明。将getNotifications的方法签名更改为async Task。你为什么要等待结果? –
lync服务器需要时间返回状态,因此匿名方法被异步调用 –