2008-09-15 54 views
1

我創建了一個獨立的程序集,其中一個類是打算通過wmi發佈的 。然後,我創建了一個窗體應用程序,該應用程序引用該程序集並嘗試發佈該類。當我嘗試 發佈課程時,我收到類型爲 的異常System.Management.Instrumentation.WmiProviderInstallationException。 異常消息顯示「異常類型 'System.Management.Instrumentation.WMIInfraException'被引發。」我有 不知道這是什麼意思。我已經嘗試過.Net2.0和.Net3.5(sp1),並獲得相同的結果。如何在.net中發佈wmi類?

下面是我的wmi類,後面跟着我用來發布它的代碼。

//Interface.cs in assembly WMI.Interface.dll 

using System; 
using System.Collections.Generic; 
using System.Text; 

[assembly: System.Management.Instrumentation.WmiConfiguration(@"root\Test", 
    HostingModel = 
System.Management.Instrumentation.ManagementHostingModel.Decoupled)] 

namespace WMI 
{ 
    [System.ComponentModel.RunInstaller(true)] 
    public class MyApplicationManagementInstaller : 
     System.Management.Instrumentation.DefaultManagementInstaller { } 

    [System.Management.Instrumentation.ManagementEntity(Singleton = true)] 
    [System.Management.Instrumentation.ManagementQualifier("Description", 
     Value = "Obtain processor information.")] 
    public class Interface 
    { 
     [System.Management.Instrumentation.ManagementBind] 
     public Interface() 
     { 
     } 

     [System.Management.Instrumentation.ManagementProbe] 
     [System.Management.Instrumentation.ManagementQualifier("Descriiption", 
      Value="The number of processors.")] 
     public int ProcessorCount 
     { 
      get { return Environment.ProcessorCount; } 
     } 
    } 
} 


//Button click in windows forms application to publish class 
try 
{ 
    System.Management.Instrumentation.InstrumentationManager.Publish(new 
WMI.Interface()); 
} 
catch (System.Management.Instrumentation.InstrumentationException 
exInstrumentation) 
{ 
    MessageBox.Show(exInstrumentation.ToString()); 
} 
catch (System.Management.Instrumentation.WmiProviderInstallationException 
exProvider) 
{ 
    MessageBox.Show(exProvider.ToString()); 
} 
catch (Exception exPublish) 
{ 
    MessageBox.Show(exPublish.ToString()); 
} 

回答

0

我用GACUTIL - installutil來測試你的類(作爲一個DLL)。該GACUTIL部分工作,但installutil(實際上mofcomp)抱怨語法錯誤:

...

錯誤語法0X80044014:在類名 意外的字符(必須是一個標識符)

編譯器返回錯誤0x80044014

...

所以我改變類的名稱爲「MyInterface的」的installutil部分工作,但類沒有返回任何實例。最後,我將主機模型更改爲網絡服務並使其正常工作。

3

總之,這是工作的最終代碼:

Provider類,在它自己的組件:

// the namespace used for publishing the WMI classes and object instances 
[assembly: Instrumented("root/mytest")] 

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Management; 
using System.Management.Instrumentation; 
using System.Configuration.Install; 
using System.ComponentModel; 

namespace WMITest 
{ 

    [InstrumentationClass(System.Management.Instrumentation.InstrumentationType.Instance)] 
    //[ManagementEntity()] 
    //[ManagementQualifier("Description",Value = "Obtain processor information.")] 
    public class MyWMIInterface 
    { 
     //[System.Management.Instrumentation.ManagementBind] 
     public MyWMIInterface() 
     { 
     } 

     //[ManagementProbe] 
     //[ManagementQualifier("Descriiption", Value="The number of processors.")] 
     public int ProcessorCount 
     { 
      get { return Environment.ProcessorCount; } 
     } 
    } 

    /// <summary> 
    /// This class provides static methods to publish messages to WMI 
    /// </summary> 
    public static class InstrumentationProvider 
    { 
     /// <summary> 
     /// publishes a message to the WMI repository 
     /// </summary> 
     /// <param name="MessageText">the message text</param> 
     /// <param name="Type">the message type</param> 
     public static MyWMIInterface Publish() 
     { 
      // create a new message 
      MyWMIInterface pInterface = new MyWMIInterface(); 

      Instrumentation.Publish(pInterface); 

      return pInterface; 
     } 

     /// <summary> 
     /// revoke a previously published message from the WMI repository 
     /// </summary> 
     /// <param name="Message">the message to revoke</param> 
     public static void Revoke(MyWMIInterface pInterface) 
     { 
      Instrumentation.Revoke(pInterface); 
     }   
    } 

    /// <summary> 
    /// Installer class which will publish the InfoMessage to the WMI schema 
    /// (the assembly attribute Instrumented defines the namespace this 
    /// class gets published too 
    /// </summary> 
    [RunInstaller(true)] 
    public class WMITestManagementInstaller : 
     DefaultManagementProjectInstaller 
    { 
    } 
} 

Windows窗體應用程序的主要形式,發佈商類:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Management; 
using System.Management.Instrumentation; 

namespace WMI 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     WMITest.MyWMIInterface pIntf_m; 

     private void btnPublish_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       pIntf_m = WMITest.InstrumentationProvider.Publish(); 
      } 
      catch (ManagementException exManagement) 
      { 
       MessageBox.Show(exManagement.ToString()); 
      } 
      catch (Exception exPublish) 
      { 
       MessageBox.Show(exPublish.ToString()); 
      } 
     } 
    } 
} 

測試Web應用程序,消費者:

using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using System.Management.Instrumentation; 
using System.Management; 

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      ManagementClass pWMIClass = null; 

      pWMIClass = new ManagementClass(@"root\interiorhealth:MyWMIInterface"); 

      lblOutput.Text = "ClassName: " + pWMIClass.ClassPath.ClassName + "<BR/>" + 
       "IsClass: " + pWMIClass.ClassPath.IsClass + "<BR/>" + 
       "IsInstance: " + pWMIClass.ClassPath.IsInstance + "<BR/>" + 
       "IsSingleton: " + pWMIClass.ClassPath.IsSingleton + "<BR/>" + 
       "Namespace Path: " + pWMIClass.ClassPath.NamespacePath + "<BR/>" + 
       "Path: " + pWMIClass.ClassPath.Path + "<BR/>" + 
       "Relative Path: " + pWMIClass.ClassPath.RelativePath + "<BR/>" + 
       "Server: " + pWMIClass.ClassPath.Server + "<BR/>"; 

      //GridView control 
      this.gvProperties.DataSource = pWMIClass.Properties; 
      this.gvProperties.DataBind(); 

      //GridView control 
      this.gvSystemProperties.DataSource = pWMIClass.SystemProperties; 
      this.gvSystemProperties.DataBind(); 

      //GridView control 
      this.gvDerivation.DataSource = pWMIClass.Derivation; 
      this.gvDerivation.DataBind(); 

      //GridView control 
      this.gvMethods.DataSource = pWMIClass.Methods; 
      this.gvMethods.DataBind(); 

      //GridView control 
      this.gvQualifiers.DataSource = pWMIClass.Qualifiers; 
      this.gvQualifiers.DataBind(); 
     } 
    } 
}