简体   繁体   English

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

[英]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: 我是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;
        }
    }
}

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 上面的无效代码是此处发布的代码的修改版本: 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. 当静态构造函数或类型的静态成员引发异常时,将发生TypeInitializationException。

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). 发生在LyncClient.GetClient()上是有道理的,因为这是为LyncClient运行静态构造函数(或初始化静态成员)的第一点。

Unfortunately, a TypeInitializationException doesn't tell us much on its own. 不幸的是,TypeInitializationException本身并不能告诉我们太多。 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. 选中后,逐步进行操作,这将在Visual Studio中打开“异常”对话框。 View the exception details and expand "InnerException". 查看异常详细信息,然后展开“ 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. 该InnerException可能具有自己的InnerException。 Get all of them, put the text in Notepad and a google search may help lead you to the culprit. 获取所有这些内容,将文本放在记事本中,然后使用Google搜索来帮助您找到罪魁祸首。 Or update your question and I'll see if I can give you a better answer with the additional context provided. 或更新您的问题,我将看看是否可以通过提供的其他上下文为您提供更好的答案。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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