none
Rx Sample of Qbservable Provider for WMI Events (LINQ to WQL)

    General discussion

  • Hi folks,

    I'm happy to share out a long awaited sample of building a Qbservable provider for reactive data sources: LINQ to WQL. Using this (very basic) provider it's possible to write queries such as the below:

     

    var res = from proc in EventProvider.Qbserve<ProcessStartTrace>()
       where proc.ProcessName == "notepad.exe"
       select new { Name = proc.ProcessName, ID = proc.ProcessID };
    

     

    Being an observable query, retrieving the query's results is not done by means of enumeration but by means of subscription. This triggers the translation of the LINQ query expression into the target language, in this case WQL. Here's a sample of receiving the events:

     

    var monitorSubscription = res.Subscribe(
     proc => {
      Console.WriteLine("Process {0} was started with ID {1}", proc.Name, proc.ID);
     }
    );
    

     

    The resulting WQL fragment is shown below, which can also be visualized by specifying a logger parameter to the EventProvider.Qbserve method (e.g. Console.Out):

     

    SELECT ProcessName, ProcessID
    FROM Win32_ProcessStartTrace
    WHERE ProcessName ="notepad.exe"
    

     

    Mapping of the ProcessStartTrace C# type to the Win32_ProcessStartTrace WMI event class is - not unsurprisingly - done using custom attributes. The sample is limited in its mapping capabilities, in that properties cannot be aliased (and hence need to have exactly the same name as it appears in the WMI event class MOF definition). This is simple to add though and pointed out in comments in the sample code.

     

    [WmiEventClass("Win32_ProcessStartTrace")]
    class ProcessStartTrace
    {
     public uint ProcessID { get; set; }
     public string ProcessName { get; set; }
    }
    

    It should be noted that the sample is meant to be as simplistic as possible, so it lacks support for various WQL constructs such as the LIKE operator. Mapping BCL constructs such as String.StartsWith to LIKE operator uses is left as a nice exercise to the reader.

    One can mix WQL-based execution with local Rx-based execution by means of the AsObservable operator. For example, to do local timestamping of events, one can use the Timestamp operator. As we don't support translating this into WQL-specific constructs, one has to use AsObservable to switch to local IObservable<T>-based execution:

     

    var res = (from proc in EventProvider.Query<ProcessStartTrace>()
       where proc.ProcessName == "notepad.exe"
       select new { Name = proc.ProcessName, ID = proc.ProcessID })
       .AsObservable()
       .Timestamp();
    

     

    Finally, the EventProvider class has different ways to formulate and execute queries. One is through a Query method which exposes a type that implements the query expression pattern (see C# specification section 7.16.3). This doesn't leverage the Qbservable infrastructure in favor of plain query operator method definitions taking in expression tree parameters:

     

    var res = from proc in EventProvider.Query<ProcessStartTrace>()
       where proc.ProcessName == "notepad.exe"
       select new { Name = proc.ProcessName, ID = proc.ProcessID };
    

     

    The Qbserve method (as shown earlier) on the other hand exposes an IQbservable<T> implementation. Expression trees built against IQbservable<T> will be turned into a query expression pattern against the type used by the Query method. See WqlQbservableSource's Subscribe method for more context.

    All of this can be downloaded from here. Please read the readme.txt file included in the ZIP file to get started with the sample. It contains important instructions on how to set up the solution and an optional custom WMI event provider.

    Have fun exploring Qbservable support in Rx! Again, notice this sample is very rudimentary. It can be improved and extended in many ways. We leave it as an exercise to the reader to do so and explore the breadth of the WQL language. As you expose more constructs through the LINQ to WQL provider, you may want to invest in creating a WQL AST code model to simplify matters. This forum is a great place to discuss this.

    Cheers,
    -Bart


    using (Microsoft.Sql.Cloud.DataProgrammability.Rx) { Signature.Emit("Bart De Smet"); }
    Wednesday, September 15, 2010 8:25 PM
    Owner

All replies

  • Why did you keep it out of light so long? Its clears out so many questions I had on this part of Reactive.

    Anyhow, thanks a million of sharing this sample.  J

    Alexander

    Friday, September 17, 2010 1:32 PM
  • Hi Alexander,

    We're in fact working on an extended "LINQ to Anything" mission. While we're figuring out the scope of this project and the timelines related to it, this sample moved to the background for a while. Enough lame apologies for now... stay tuned to hear more about "LINQ to Anything" materializing in the not so distant future.

    Thanks,
    -Bart


    using (Microsoft.Sql.Cloud.DataProgrammability.Rx) { Signature.Emit("Bart De Smet"); }
    Friday, September 17, 2010 5:12 PM
    Owner
  • Bart,

    I will be getting my hands dirty once its materialized to explore. Some great video tutorials to "LINQ to Anything" , like in past series, would be greatly appreciated.   ;-)

    Alexander

    Friday, September 17, 2010 6:43 PM
  • Hello Bart,

    I can not build the WMI demo,

    I am using .Net 4.0 and I have tried Rx v1.0.2838.0 and v1.0.2787.0,

    the error is "The type arguments for method 'System.Linq.Observable.Publish<TSource,TResult>(System.IObservable<TSource>, System.Collections.Generic.ISubject<TSource,TResult>)' cannot be inferred from the usage. Try specifying the type arguments explicitly. File: D:\Projects\2010\C#\Rx\WMI sample\Demo\Program.cs, Line: 76, Column: 21, Project: Demo",

    any advice?

    the video is awesome,

    Thanks a lot,

    Klaus.

    Sunday, February 6, 2011 8:21 AM
  • That extension method was removed in the last release.

    You can read here for more information.

    http://social.msdn.microsoft.com/Forums/en-US/rx/thread/2805dd0d-764a-4669-876d-18e7f4c2828c

    http://social.msdn.microsoft.com/Forums/en-US/rx/thread/468d6fd1-c095-46e9-8bc6-21d0ac702ccb

    It looks like they will be back in the next version (soon).


    James Miles http://enumeratethis.com
    Sunday, February 6, 2011 11:16 AM
  • Thank you, James,

    I will have a look when the new version will be available,

    Klaus.

    Sunday, February 6, 2011 5:07 PM
  • The sample has now been updated for the v1.0.10621 release. More information on IQbservable<T> providers will come up later. Stay tuned!
    using (Microsoft.Sql.Cloud.DataProgrammability.Rx) { Signature.Emit("Bart De Smet"); }
    Monday, July 18, 2011 10:57 PM
    Owner
  • Hi,

    Is there an update to the sample for Rx v2 ? I upgraded all the references to 2.2.5.0 but it runs but no longer works.

    many thanks

    mark.

    Thursday, April 2, 2015 8:02 AM