-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added IWMIWatcher interface so WMIWatcher can be injected via DI
- Loading branch information
Nicolas
authored and
Nicolas
committed
Dec 2, 2020
1 parent
f3289d8
commit e454bea
Showing
12 changed files
with
242 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Worker"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
<UserSecretsId>dotnet-ORMi.Sample.Worker-C66EC170-4903-4DCA-B88C-AD394F5DF5F3</UserSecretsId> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.8" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\ORMi.Sample\ORMi.Sample.csproj" /> | ||
<ProjectReference Include="..\ORMi\ORMi.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using ORMi.Interfaces; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace ORMi.Sample.Worker | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
CreateHostBuilder(args).Build().Run(); | ||
} | ||
|
||
public static IHostBuilder CreateHostBuilder(string[] args) => | ||
Host.CreateDefaultBuilder(args) | ||
.ConfigureServices((hostContext, services) => | ||
{ | ||
services.AddHostedService<Worker>(); | ||
services.AddSingleton<IWMIHelper>(new WMIHelper("root\\cimv2")); | ||
services.AddSingleton<IWMIWatcher, WMIWatcher>(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"profiles": { | ||
"ORMi.Sample.Worker": { | ||
"commandName": "Project", | ||
"environmentVariables": { | ||
"DOTNET_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using ORMi.Interfaces; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace ORMi.Sample.Worker | ||
{ | ||
public class Worker : BackgroundService | ||
{ | ||
private readonly IWMIHelper _helper; | ||
private readonly IWMIWatcher _watcher; | ||
|
||
public Worker(IWMIHelper helper, IWMIWatcher watcher) | ||
{ | ||
_helper = helper; | ||
_watcher = watcher; | ||
} | ||
|
||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
_watcher.Initialize("root\\CimV2", "SELECT * FROM Win32_ProcessStartTrace"); | ||
_watcher.WMIEventArrived += _watcher_WMIEventArrived; | ||
} | ||
|
||
private void _watcher_WMIEventArrived(object sender, WMIEventArgs e) | ||
{ | ||
dynamic process = e.Object; | ||
|
||
Console.WriteLine("New Process: {0} (Pid: {1})", process.ProcessName, process.ProcessID.ToString()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Management; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ORMi.Interfaces | ||
{ | ||
public interface IWMIWatcher | ||
{ | ||
event WMIWatcher.WMIEventHandler WMIEventArrived; | ||
|
||
/// <summary> | ||
/// Disposes the WMIWatcher object. | ||
/// </summary> | ||
void Dispose(); | ||
|
||
/// <summary> | ||
/// Initializes the WMIWatcher with the desired parameters. | ||
/// </summary> | ||
/// <param name="scope">Desired Scope</param> | ||
/// <param name="query">Query to be watch</param> | ||
/// <param name="type">Type of result</param> | ||
/// <param name="options">Connection options. If null, default options are used</param> | ||
void Initialize(string scope, string query, Type type = null, ConnectionOptions options = null); | ||
|
||
/// <summary> | ||
/// Starts the current WMI Event watcher | ||
/// </summary> | ||
void StartWatcher(); | ||
|
||
/// <summary> | ||
/// Stops the current WMI Event watcher | ||
/// </summary> | ||
void StopWatcher(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.