简体   繁体   中英

C# LyncClient: Getting a Lync user's presence info

I am a newbie at C# & am having a hard time figuring out what is wrong in the following code:

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using Microsoft.Lync.Model;

namespace test3
{
    class Program
    {
        static void Main(string[] args)
        {
            LyncClient lyncClient;

            try
            {
                lyncClient = LyncClient.GetClient();
            }
            catch (ClientNotFoundException clientNotFoundException)
            {
                Console.WriteLine(clientNotFoundException);
                return;
            }
            catch (NotStartedByUserException notStartedByUserException)
            {
                Console.Out.WriteLine(notStartedByUserException);
                return;
            }
            catch (LyncClientException lyncClientException)
            {
                Console.Out.WriteLine(lyncClientException);
                return;
            }
            catch (SystemException systemException)
            {
                if (IsLyncException(systemException))
                {
                    // Log the exception thrown by the Lync Model API.
                    Console.WriteLine("Error: " + systemException);
                    return;
                }
                else
                {
                    // Rethrow the SystemException which did not come from the Lync Model API.
                    throw;
                }
            }

            Console.WriteLine("LYNC CLIENT STATE: ", lyncClient.State);

            // GET AVAILABILITY
            ContactAvailability currentAvailability = 0;

            try
            {
                currentAvailability = (ContactAvailability)
                                                          lyncClient.Self.Contact.GetContactInformation(ContactInformationType.Availability);
                Console.WriteLine("***********Console.WriteLine(currentAvailability)*********");

                // https://code.msdn.microsoft.com/lync/Lync-2013-Use-the-Lync-47ded7b4
                // https://msdn.microsoft.com/en-us/library/office/jj933083.aspx
                // https://msdn.microsoft.com/en-us/library/office/jj933159.aspx
                lyncClient.ContactManager.BeginSearch(
                "Humpty,Dumpty",
                (ar) =>
                {
                    SearchResults searchResults = lyncClient.ContactManager.EndSearch(ar);
                    if (searchResults.Contacts.Count > 0)
                    {
                        Console.WriteLine(
                            searchResults.Contacts.Count.ToString() +
                            " found");

                        foreach (Contact contact in searchResults.Contacts)
                        {
                            Console.WriteLine(
                                contact.GetContactInformation(ContactInformationType.DisplayName).ToString());
                            currentAvailability = (ContactAvailability)
                                                          contact.GetContactInformation(ContactInformationType.Availability);
                            Console.WriteLine(currentAvailability);
                        }
                    }
                },
                null);

                Console.WriteLine(ContactInformationType.Availability);
                Console.WriteLine(lyncClient.Self.ToString());
            }
            catch (LyncClientException e)
            {
                Console.WriteLine(e);
            }
            catch (SystemException systemException)
            {
                if (IsLyncException(systemException))
                {
                    // Log the exception thrown by the Lync Model API.
                    Console.WriteLine("Error: " + systemException);
                }
                else
                {
                    // Rethrow the SystemException which did not come from the Lync Model API.
                    throw;
                }
            }

        }

        static private bool IsLyncException(SystemException ex)
        {
            return
                ex is NotImplementedException ||
                ex is ArgumentException ||
                ex is NullReferenceException ||
                ex is NotSupportedException ||
                ex is ArgumentOutOfRangeException ||
                ex is IndexOutOfRangeException ||
                ex is InvalidOperationException ||
                ex is TypeLoadException ||
                ex is TypeInitializationException ||
                ex is InvalidComObjectException ||
                ex is InvalidCastException;
        }
    }
}

I see the following in the output logs:

The thread 0x32d4 has exited with code 259 (0x103).
The thread 0x31ec has exited with code 259 (0x103).
The thread 0x28d0 has exited with code 259 (0x103).
'test3.vshost.exe' (CLR v4.0.30319: test3.vshost.exe): Loaded 'c:\users\sw029693\documents\visual studio 2013\Projects\test3\test3\bin\Debug\test3.exe'. Symbols loaded.
'test3.vshost.exe' (CLR v4.0.30319: test3.vshost.exe): Loaded 'c:\users\sw029693\documents\visual studio 2013\Projects\test3\test3\bin\Debug\Microsoft.Lync.Model.dll'. Cannot find or open the PDB file.
Step into: Stepping over non-user code 'test3.Program.<>c__DisplayClass2..ctor'
'test3.vshost.exe' (CLR v4.0.30319: test3.vshost.exe): Loaded 'C:\windows\Microsoft.Net\assembly\GAC_MSIL\Accessibility\v4.0_4.0.0.0__b03f5f7f11d50a3a\Accessibility.dll'. Cannot find or open the PDB file.
A first chance exception of type 'System.TypeInitializationException' occurred in test3.exe
The thread 0xcd8 has exited with code 259 (0x103).
The thread 0x3580 has exited with code 259 (0x103).
The program '[9700] test3.vshost.exe' has exited with code 0 (0x0).

This seems to be broken at the following code:

lyncClient = LyncClient.GetClient();

The following are the references i have:

Project References

Any thoughts?

The above code which does not work is modified version of the code posted here: https://code.msdn.microsoft.com/lync/Lync-2013-Use-the-Lync-47ded7b4

the code in the link works. I am unable to decipher what is missing in my version.. Please help!

It could be many different things. A TypeInitializationException occurs when the static constructor or a static member of a type throws an exception.

It makes sense that this would occur on LyncClient.GetClient(), because this is the first point at which the static constructor would run for LyncClient (or a static member would be initialized).

Unfortunately, a TypeInitializationException doesn't tell us much on its own. If you were to print the exception message, you'd see generic information about the type that failed but not necessarily anything useful beyond that.

What I'd do is set a breakpoint on that line of code. Once you hit it, step through, which will open up an Exception dialog in Visual Studio. View the exception details and expand "InnerException". This will contain the actual exception that was thrown (and I'll wager a guess it's going to be something along the lines of an exception related to a reference within your project). That InnerException may have its own InnerException. Get all of them, put the text in Notepad and a google search may help lead you to the culprit. Or update your question and I'll see if I can give you a better answer with the additional context provided.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM