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

时间:2016-09-16 21:58:04

标签: c# .net lync lync-2013 lync-client-sdk

我是C#& amp;的新手我很难搞清楚以下代码中的错误:

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

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

1 个答案:

答案 0 :(得分:0)

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

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

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

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

祝你好运!