Я пытаюсь получить статус присутствия lync данного пользователя. Мой код будет разговаривать с lync-сервером 2010, используя UCMA 4.0 в 64-битном env.Ждите уведомления асинхронный вызов
вот мой код, чтобы ждать асинхронного вызова, чтобы получить статус 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);
}
}
}
}
Не использовать асинхронные пустоты. См. Статью [this] (https://msdn.microsoft.com/en-us/magazine/jj991977.aspx?f=255&MSPPError=-2147217396) для получения полного объяснения. Измените подпись метода getNotifications на async Task. Почему вы хотите дождаться результатов? –
Сервер lync требует времени, чтобы вернуть статус, поэтому анонимный метод называется асинхронным. –