2016-09-16 78 views
0

我在C#&一個新手,我有一個很難搞清楚什麼是錯在下面的代碼:C#LyncClient:獲取的Lync用戶的狀態信息

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(); 

的FOL降脂是引用我有:

Project References

有什麼想法?在鏈接的作品https://code.msdn.microsoft.com/lync/Lync-2013-Use-the-Lync-47ded7b4

代碼:

上面的代碼不工作被修改,張貼在這裏的代碼版本。我無法破譯我的版本中缺少的東西。請幫助!

回答

0

可能是很多不同的東西。當靜態構造函數或類型的靜態成員引發異常時,會發生TypeInitializationException。

它是有道理的,這將發生在LyncClient.GetClient(),因爲這是靜態構造函數運行LyncClient(或靜態成員將被初始化)的第一個點。

不幸的是,一個TypeInitializationException並不能告訴我們多少。如果要打印異常消息,則會看到關於失敗類型的通用信息,但不一定有用。

我會做的是在該行代碼上設置一個斷點。一旦你點擊它,就會在Visual Studio中打開一個Exception對話框。查看異常詳細信息並展開「InnerException」。這將包含拋出的實際異常(並且我會猜測這將是與項目中的引用有關的異常的行)。該InnerException可能有其自己的InnerException。把所有這些文字放在記事本中,谷歌搜索可能會幫助你找到罪魁禍首。或者更新你的問題,我會看看我能否提供更好的答案。

祝你好運!