[英]How to subscribe to events using Microsoft.Management.Infrastructure in a NetCore application?
I'd like to know how to subscribe and receive events in a NetCore application using the library Microsoft.Management.Infrastructure.我想知道如何使用 Microsoft.Management.Infrastructure 库在 NetCore 应用程序中订阅和接收事件。
So far I'm able to query some data using something like this:到目前为止,我可以使用以下方式查询一些数据:
var options = new CimSessionOptions();
var securePassword = new SecureString();
foreach (var c in password)
securePassword.AppendChar(c);
var credentials = new CimCredential(PasswordAuthenticationMechanism.Default, "", "user", securePassword);
options.AddDestinationCredentials(GetCredentials(user, password));
CimSession session = CimSession.Create(url, options);
var queryInstance = mySession.QueryInstances("a namespace", "WQL", "a query");
foreach (var instance in queryInstance)
Console.WriteLine(instance.ToString());
Searching on Internet, I saw examples doing this using System.Management, like the following one:在 Internet 上搜索,我看到了使用 System.Management 执行此操作的示例,如下所示:
ManagementEventWatcher watcher = new ManagementEventWatcher("namespace", "query");
watcher.EventArrived += new EventArrivedEventHandler(EventArrived);
watcher.Start();
But unfortunately I can't use this package, I just need the equivalent but using MMI.但不幸的是我不能使用这个 package,我只需要等效但使用 MMI。
I've seen some class that maybe could help me in this task like CimIndicationWatcher which is part of the Microsoft.Management.Infrastructure.CimCmdlets package, however I imported it to my project but I can't access any of the classes in it.我已经看到一些 class 可能可以帮助我完成这项任务,例如CimIndicationWatcher ,它是 Microsoft.Management.Infrastructure.CimCmdlets package 的一部分,但是我将它导入到我的项目中,但我无法访问其中的任何类。
There's a lack of documentation/examples about the use of this class or this scenario in general.缺少有关使用此 class 或一般情况下的文档/示例。
I found a way, not sure if the correct one, but I'm able to receive events using Microsoft.Management.Infrastructure library.我找到了一种方法,不确定是否正确,但我能够使用 Microsoft.Management.Infrastructure 库接收事件。
This is how I did it:我是这样做的:
/// Create an observer class
class CimInstanceWatcher : IObserver<CimSubscriptionResult>
{
public void OnCompleted()
{
Console.WriteLine("Done");
}
public void OnError(Exception e)
{
Console.WriteLine("Error: " + e.Message);
}
public void OnNext(CimSubscriptionResult value)
{
Console.WriteLine("Event received: " + e.Message);
}
}
Now, use it in this way:现在,以这种方式使用它:
static void Main(string[] args)
{
CimSession session = CimSession.Create("MY-PC");
// create a watcher
var watcher= new CimInstanceWatcher();
var queryInstance = GetValues(session).Subscribe(watcher);
// close application once events are no longer required to be received
Console.ReadLine();
session.Close();
}
public static CimAsyncMultipleResults<CimSubscriptionResult> GetValues(CimSession _session)
{
// Subscribe to any event, in this case, fire an event when an application is opened
return _session.SubscribeAsync(@"root\cimv2", "WQL", "SELECT targetInstance.Name FROM __InstanceCreationEvent within 2 " +
"WHERE targetinstance isa \"Win32_Process\"");
}
Now events will be registered on the OnNext
method, if there's an error it will also be prompted to the observer.现在将在
OnNext
方法上注册事件,如果有错误也会提示给观察者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.