C# LyncClient:获取 Lync 用户的状态信息

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

我是 C# 的新手,很难弄清楚以下代码中的错误所在:

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;
        }
    }
}

我在输出日志中看到以下内容:

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).

这似乎在以下代码处被破坏:

lyncClient = LyncClient.GetClient();

以下是我的参考资料:

Project References

有什么想法吗?

以上不起作用的代码是此处发布的代码的修改版本:https://code.msdn.microsoft.com/lync/Lync-2013-Use-the-Lync-47ded7b4

link 中的代码有效。我无法破译我的版本中缺少的内容。请帮忙!

它可能是 许多 不同的东西。当静态构造函数或类型的静态成员抛出异常时,会发生 TypeInitializationException。

这发生在 LyncClient.GetClient() 上是有道理的,因为这是静态构造函数 运行 用于 LyncClient(或初始化静态成员)的第一个点。

不幸的是,TypeInitializationException 本身并不能告诉我们太多信息。如果您要打印异常消息,您会看到有关失败类型的一般信息,但除此之外不一定有用。

我要做的是在那行代码上设置一个断点。一旦你点击它,单步执行,这将在 Visual Studio 中打开一个异常对话框。查看异常详细信息并展开 "InnerException"。这将包含抛出的实际异常(我敢打赌,这将是与项目中的引用相关的异常)。该 InnerException 可能有自己的 InnerException。获取所有这些,将文本放入记事本中,google 搜索可能会帮助您找到罪魁祸首。或者更新您的问题,我会看看是否可以根据提供的其他上下文为您提供更好的答案。

祝你好运!