Ich bin ein Neuling zu C# & Ich versuche, das Lync SDK zu verwenden, um programmgesteuert nach einem Lync-Benutzer zu suchen, um ihren Status zu erhalten. Ich bin nicht sicher, wie die Ergebnisse an die Webservice-Antwort übergeben werden, wenn der asynchrone Rückruf ausgeführt wird.C# Wie zeichne ich asynchrone Ergebnisse auf?
Dies ist der Code:
Webservice-Controller GET Endpunkt:
// 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";
}
Die obige Methode ruft das Business Service lync.search (..), die wie folgt lautet:
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);
}
Ich habe versucht, Task + zu verwenden, aber es fällt mir schwer herauszufinden, wie ich die Ergebnisse aus der Callback-Funktion als Ergebnis der Suchmethode zurückgeben kann. Wie erfasse ich das in der Controller-Klasse?
Bitte beachten Sie, dass die Verwendung von Async-Code eine Tendenz hat, Ihren eigenen Code async zu machen. Wahrscheinlich werden Sie Ihre Methode in zwei Teile aufteilen, den ursprünglichen Code, der zum asynchronen Aufruf führt, und was auch immer als Antwort auf diesen Aufruf ausgeführt werden sollte. Liefern die betreffenden Objekte und Methoden "Task" oder "Task" zurück, verwenden sie "IAsyncResult" oder nur einen Callback-Mechanismus? –
Ich glaube, dass die Lync API den IAsyncResult-Mechanismus verwendet – Rookie
Wie ändere ich die Suchmethode, so dass ich stattdessen eine Aufgabe zurückgibt? – Rookie