Created
February 3, 2020 12:50
-
-
Save stroheim/f59270fa3d10e2299e8bb2d8dfe58348 to your computer and use it in GitHub Desktop.
ChangeToken sample
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
using Microsoft.Extensions.DependencyInjection; | |
namespace ConsoleApp3 | |
{ | |
public static class FileMonitorExtension | |
{ | |
public static IServiceCollection AddFileMonitor(this IServiceCollection services) | |
{ | |
return services.AddSingleton<IFileMonitorService, FileMonitorService>(); | |
} | |
} | |
} |
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
using System; | |
using System.IO; | |
using System.Linq; | |
using Microsoft.Extensions.Hosting; | |
using Microsoft.Extensions.Primitives; | |
namespace ConsoleApp3 | |
{ | |
public class FileMonitorService : IFileMonitorService | |
{ | |
private readonly IHostEnvironment _environment; | |
public FileMonitorService( | |
IHostEnvironment environment) | |
{ | |
_environment = environment; | |
} | |
public void Initialize(string rootDirectory) | |
{ | |
var files = Directory.GetFiles(rootDirectory, "*.sql", SearchOption.AllDirectories) | |
.Select(s => Path.IsPathRooted(s) | |
? Path.GetRelativePath(Directory.GetCurrentDirectory(), s) | |
: s); | |
foreach (var file in files) | |
{ | |
ChangeToken.OnChange( | |
()=>_environment.ContentRootFileProvider.Watch(file), | |
(state) => | |
{ | |
Console.WriteLine($"file changed: {state}"); | |
}, | |
file); | |
Console.WriteLine($"filePath: {file}"); | |
} | |
} | |
} | |
} |
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
namespace ConsoleApp3 | |
{ | |
public interface IFileMonitorService | |
{ | |
void Initialize(string rootDirectory); | |
} | |
} |
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
using System.IO; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Microsoft.Extensions.Hosting; | |
namespace ConsoleApp3 | |
{ | |
public class MainProcess : IHostedService | |
{ | |
private readonly IFileMonitorService _fileMonitorService; | |
public MainProcess(IFileMonitorService fileMonitorService) | |
{ | |
_fileMonitorService = fileMonitorService; | |
} | |
public async Task StartAsync(CancellationToken cancellationToken) | |
{ | |
await Task.Delay(1000, cancellationToken); | |
_fileMonitorService.Initialize(Path.Combine(Directory.GetCurrentDirectory(), "Resources")); | |
} | |
public async Task StopAsync(CancellationToken cancellationToken) | |
{ | |
await Task.Delay(1000, cancellationToken); | |
} | |
} | |
} |
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
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Hosting; | |
namespace ConsoleApp3 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
CreateHostBuilder(args).Build().Run(); | |
} | |
public static IHostBuilder CreateHostBuilder(string[] args) => | |
Host.CreateDefaultBuilder(args) | |
.ConfigureServices((context, services) => | |
{ | |
services.AddFileMonitor(); | |
services.AddHostedService<MainProcess>(); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment