I'm trying to use Async versions of Microsoft.Management.Infrastructure, but I'm having hard time figuring out how to do session management with Reactive Extensions.
Here's some background:
- One can create a CIM session using
CimSession.CreateAsync, which returnsIObservable CimSession.CreateAsyncreturns aCimAsyncResult<T>, which wrapsCimAsyncDelegatedObservable<T>CimAsyncDelegatedObservable<T>does not disposeCimSessionat any point, and thus one must manually disposeCimSessionwhen it is not needed anymore.- After getting a handle to
CimSession, I'm trying to run a WQL query usingQueryInstancesAsync, for examplesession.QueryInstancesAsync(@"root\cimv2", "WQL", "SELECT Username FROM Win32_ComputerSystem")
If CimSession returned Tasks instead of Observables, the code would be pretty straightforward:
using var session = CimSession.CreateAsync(".");
var results = session.QueryInstancesAsync(@"root\cimv2", "WQL", "SELECT Username FROM Win32_ComputerSystem");
But as the Async methods are implemented using Observables, I'm not sure how to translate this to idiomatic usage of Observables. Sure I could translate my methods to Tasks using System.Reactive.Linq.Observable.ForEachAsync, but I'd like to learn how to use Observables properly.
To sum it up, my questions would be:
- Bind lifetime of a
CimSessiontoObservable - How to make sure
CimSessiongets disposed, if there are no subscribers? Should I force at least one subscriber, but having factory method? - How to dispose
CimSessionobservable when results forQueryInstancesAsyncarrive?