Skip to content

Instantly share code, notes, and snippets.

View mr5z's full-sized avatar
🎯
Focusing

mark mr5z

🎯
Focusing
View GitHub Profile
@mr5z
mr5z / MonitorController.cs
Created May 10, 2021 08:30
GET api/monitor
[Route("api/[controller]")]
[ApiController]
public class MonitorController : Controller
{
private readonly IActionDescriptorCollectionProvider _provider;
public MonitorController(IActionDescriptorCollectionProvider provider)
{
_provider = provider;
}
@mr5z
mr5z / Observable.cs
Last active May 9, 2021 13:25
Property event to observable.
class Observable
{
private object actionCollection = null!;
private MethodInfo? methodInfo;
public ActionCollection<TModel> From<TModel>(TModel target) where TModel : INotifyPropertyChanged
{
RegisterPropertyChanged(target);
return BuildActionCollection<TModel>();
}
class LoggingInterceptor : HttpClientHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
Debug.Log("Request {0}", request.RequestUri.PathAndQuery);
if (request.Content != null)
{
var contentRequest = await request.Content.ReadAsStringAsync();
}
@mr5z
mr5z / Configuration.cs
Last active April 26, 2021 14:05
Scheduled closing of config file
public class Configuration : IConfiguration
{
private readonly string assemblyResourceFile;
private AutoDisposeJsonDocument? smartJsonDocument;
public Configuration(string assemblyResourceFile)
{
this.assemblyResourceFile = assemblyResourceFile;
}
@mr5z
mr5z / AutoDisposeJsonDocument.cs
Created April 26, 2021 11:18
Scheduled disposable objects to be registered in an IoC container where IDisposable is disallowed.
private void ScheduleDisposeResourceStreams()
{
_ = Task.Factory.StartNew(async () =>
{
await Task.Delay(TimeSpan.FromMinutes(1));
// `stream` creation happens right after the invocation of this
// They are in the same thread so no lock needed
jsonStream?.Dispose();
jsonDocument?.Dispose();
jsonStream = null;
@mr5z
mr5z / PleaseHelp.cs
Created April 24, 2021 11:50
JSON Path to Object
public async Task<T?> GetValue<T>(string path, JsonSerializerOptions? options, CancellationToken cancellationToken)
{
using var stream = GetStream();
using var jsonDocument = await JsonDocument.ParseAsync(stream, cancellationToken: cancellationToken);
var root = jsonDocument.RootElement;
var jsonPath = JsonPath.Parse(path);
var pathResult = jsonPath.Evaluate(root);
if (pathResult.Error != null)
throw new InvalidOperationException(pathResult.Error);
@mr5z
mr5z / RateLimiter.cs
Last active April 21, 2021 15:48
hypersapien
//Startup.cs
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseIpRateLimiting();
app.UseClientRateLimiting();
// the rest
}
public void ConfigureServices(IServiceCollection services)
@mr5z
mr5z / productrest.cs
Last active April 20, 2021 12:22
Y_Y
IProductGet : IGet<List<Product>>
IProductPost : IPost<Product, Product>
IProductDelete : IDelete<Product, long>
class ProductRest :
IProductGet,
IProductPost,
IProductDelete
{
public async Task<List<Product>> Get(CancellationToken cancellationToken)
@mr5z
mr5z / neil.php
Last active March 26, 2021 14:19
1k lang
<html>
<head>
<style>
#customers {
font-family: Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}
#customers td, #customers th {
@mr5z
mr5z / AdaptiveCommand.cs
Created March 2, 2021 13:29
Smart and Non-async/Async implementation of ICommand
// You need Prism though :)
public class AdaptiveCommand : DelegateCommand
{
private const double DefaultInvocationDelay = 0.7;
public double InvocationDelayInSeconds { get; set; }
private DateTime lastInvokeTime = DateTime.MinValue;
private readonly Func<bool>? canExecute;