简体   繁体   English

如何在 NetCore 应用程序中使用 Microsoft.Management.Infrastructure 订阅事件?

[英]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.

相关问题 Microsoft.Management.Infrastructure - Where the Microsoft.Management.Infrastructure 使用Microsoft.Management.Infrastructure(CimSession)读取注册表值 - Read Registry Values using Microsoft.Management.Infrastructure (CimSession) Microsoft.Management.Infrastructure命名空间 - Cim类 - Microsoft.Management.Infrastructure Namespace - Cim classes c# Microsoft.Management.Infrastructure - c# Microsoft.Management.Infrastructure 如何使用 Microsoft.Management.Infrastructure (MMI) 而不是 System.Management 进行本地 WMI 查询 - How to use Microsoft.Management.Infrastructure (MMI) instead of System.Management for local WMI queries 使用 Microsoft.Management.Infrastructure 检索串行端口信息 - Retrieve Serial Port information with Microsoft.Management.Infrastructure 无法加载文件或程序集“Microsoft.Management.Infrastructure - Could not load file or assembly 'Microsoft.Management.Infrastructure 从 .Net Core 运行 Powershell - 无法加载文件或程序集 Microsoft.Management.Infrastructure - Running Powershell from .Net Core - Could not load file or assembly Microsoft.Management.Infrastructure 借助microsoft.management.infrastructure命名空间在C#中使用WMI创建快照 - Create Snapshot with WMI in C# with help of the microsoft.management.infrastructure namespace C#需要访问Microsoft管理基础结构名称空间 - C# Need to access Microsoft Management Infrastructure namespace
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM