【发布时间】:2016-09-20 16:27:38
【问题描述】:
我是 C# 的新手,我正在尝试使用 Lync SDK 以编程方式搜索 Lync 用户以获取他们的状态。我不确定在执行异步回调时如何将结果传递给 web 服务响应。
这是代码:
Web 服务控制器 GET 端点:
// GET: api/Lync/5
public String Get(int id)
{
log.Info("[GET] Search for Lync User: " + id);
Lync lync = new Lync();
////lync.signIn();
Boolean isSignedIn = lync.isUserSignedIn();
if (isSignedIn)
{
log.Info("User is Signed In");
Console.Write("Enter search key : ");
lync.Search("medina");
//lync.Search("medina",
//(lyncContacts) =>
//{
// log.Debug("Search Results Callback fired!");
// log.Info("Results found: " + lyncContacts.Count);
// return lyncContacts.ToString();
//});
//Console.WriteLine(name);
//Console.ReadLine();
return "testUser";
}
else
{
log.Info("User is not Signed In!");
// TODO: Return status 500
return "testUser";
}
//Console.ReadLine();
//return "testUser";
}
上述方法调用业务服务lync.search(..)如下:
public void Search(string searchKey)
{
List<LyncContact> contactList = new List<LyncContact>();
//List<ContactInformationType> ContactInformationList = new List<ContactInformationType>();
//ContactInformationList.Add(ContactInformationType.Activity);
//ContactInformationList.Add(ContactInformationType.Availability);
// ContactInformationList.Add(ContactInformationType.CapabilityString);
//ContactSubscription contactSubscription = LyncClient.GetClient().ContactManager.CreateSubscription();
Console.WriteLine("Searching for contacts on " + searchKey);
LyncClient.GetClient().ContactManager.BeginSearch(
searchKey,
(ar) =>
{
SearchResults searchResults = LyncClient.GetClient().ContactManager.EndSearch(ar);
if (searchResults.Contacts.Count > 0)
{
log.Info("Search results found: " + searchResults.Contacts.Count);
Console.WriteLine(searchResults.Contacts.Count.ToString() + " found");
foreach (Contact contact in searchResults.Contacts)
{
String displayName = contact.GetContactInformation(ContactInformationType.DisplayName).ToString();
ContactAvailability currentAvailability = (ContactAvailability)contact.GetContactInformation(ContactInformationType.Availability);
Console.WriteLine(
contact.GetContactInformation(ContactInformationType.DisplayName).ToString() + " " + contact.GetContactInformation(ContactInformationType.Availability).ToString());
log.Debug("Display Name: " + displayName);
log.Debug("Availability: " + currentAvailability);
LyncContact lyncContact = new LyncContact.Builder().DisplayName("Snehil").Availability("Busy").build();
contactList.Add(lyncContact);
//done(contactList);
}
return;
}
else
{
log.Info("No Results found!");
//done(contactList);
return;
}
},
null);
} else
{
log.Info("No Results found!");
//done(contactList);
return;
}
},
null);
}
我尝试使用 Task+await,但我很难弄清楚如何将回调函数的结果作为搜索方法的结果返回。我如何在控制器类中捕获它?
【问题讨论】:
-
请注意,使用异步代码也会使您自己的代码异步。您可能会将您的方法分成两部分,导致异步调用的初始代码,以及响应此调用返回应执行的任何内容。有问题的对象和方法是否返回
Task或Task<T>,它们是使用IAsyncResult,还是仅使用回调机制? -
我认为lync api使用的是IAsyncResult机制
-
如何更改搜索方法以便我返回任务?
-
TaskFactory.FromAsync如果您必须使用无法更改的使用IAsyncResult的现有代码,或者如果可以更改则只需重写代码。对于现有代码,您可能希望创建一个抽象层来隐藏其他框架的血腥。 -
为什么不直接调用
EndSearch,而不是使用回调?
标签: c# asynchronous lync-2013 lync-client-sdk