Skip to content

Instantly share code, notes, and snippets.

@DalSoft
DalSoft / ValidateEmpty.cs
Created July 1, 2022 11:51
Validate Empty using C# expressions
// Usage ValidateEmpty(x => x.Id),
public static ValidationResult ValidateEmpty<T, TProp>(this T o, Expression<Func<T,TProp>> propertySelector)
{
MemberExpression body = (MemberExpression)propertySelector.Body;
var propInfo = body.Member as PropertyInfo;
var value = propInfo?.GetValue(o, null);
return value switch
{
@DalSoft
DalSoft / GetMimeTypeForFileExtension.cs
Created July 1, 2022 11:49
GetMimeTypeForFileExtension
private static string GetMimeTypeForFileExtension(string filePath)
{
const string defaultContentType = "application/octet-stream";
if (!new FileExtensionContentTypeProvider().TryGetContentType(filePath, out string contentType))
{
contentType = defaultContentType;
}
return contentType;
}
@DalSoft
DalSoft / webpack.config.js
Last active January 14, 2020 13:20
Webpack with multiple entries
const glob = require('glob');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const htmlPlugins = generateHtmlPlugins();
function generateHtmlPlugins() {
@DalSoft
DalSoft / app.html
Last active December 5, 2019 10:57 — forked from lstarky/app.html
Test
<template>
<require from="styles.css" />
<require from="tree-view" />
<tree-view />
</template>
@DalSoft
DalSoft / ValidateCertificateChain.cs
Created July 10, 2019 13:37
ValidateCertificateChain for custom Root CA
private static bool ValidateCertificateChain(X509Certificate certificate)
{
var chain = new X509Chain();
var root = new X509Certificate2("ca.cer"); // Root CA of Self signed cert
var cert = new X509Certificate2(certificate);
chain.Reset(); // Not sure is this is needed
chain.ChainPolicy.VerificationFlags = X509VerificationFlags.IgnoreRootRevocationUnknown;
chain.ChainPolicy.RevocationMode = X509RevocationMode.Online; // Check with our own CA if the cert has been revoked
chain.ChainPolicy.ExtraStore.Add(root); // Self signed Root CA
@DalSoft
DalSoft / bip39+bip32.cs
Created November 28, 2018 11:32 — forked from imyourm8/bip39+bip32.cs
BIP39 & BIP32 C# example implementation
/*
This code allows you to export BIP39 12 words phrase and use it with Ethereum/Loom
To check Popular derivative paths look here https://www.myetherwallet.com/#view-wallet-info
Or here is quick cheatsheet https://gyazo.com/a4199116750b57a02917eb255d4c033e
Dependency on very nice library https://github.com/MetacoSA/NBitcoin - clone and build it
*/
using NBitcoin;
@DalSoft
DalSoft / DalSoftDbContext.cs
Last active March 12, 2018 07:41
Entity Framework Core Migrations and Seeding
using System;
using System.IO;
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.Extensions.Configuration;
namespace DalSoft.Data
@DalSoft
DalSoft / Startup.cs
Last active November 25, 2015 16:53
Professional WebApi Part 1 - WebApi bootstrap without any bloat using Owin
public class Startup
{
public void Configuration(IAppBuilder app)
{
var config = new HttpConfiguration();
var jsonSerializerSettings = config.Formatters.JsonFormatter.SerializerSettings;
//Remove unix epoch date handling, in favor of ISO
jsonSerializerSettings.Converters.Add(new IsoDateTimeConverter { DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fff" });
@DalSoft
DalSoft / CreateMessage.cs
Last active August 29, 2015 14:21
DalSoft.RestClient - dynamic C# rest client in action PushWoosh SDK
// Method /createMessage https://www.pushwoosh.com/programming-push-notification/pushwoosh-push-notification-remote-api/#PushserviceAPI-Method-messages-create
dynamic pushwoosh = new RestClient("https://cp.pushwoosh.com/json/1.3");
var pushWooshResponse = await pushwoosh.CreateMessage.Post(new
{
request = new
{
application = "APPLICATION_CODE",
auth = "API_ACCESS_TOKEN",
notifications = new[] { new {
send_date = "now", // YYYY-MM-DD HH:mm OR 'now'
@DalSoft
DalSoft / Put.cs
Last active June 3, 2021 09:28
Introducing DalSoft.RestClient the dynamic rest client
dynamic client = new RestClient("http://jsonplaceholder.typicode.com");
var post = new { title = "foo", body = "bar", userId = 10 };
var result = await client.Posts(1).Put(post);
Assert.That(result.title, Is.EqualTo(post.title));
Assert.That(result.body, Is.EqualTo(post.body));
Assert.That(result.userId, Is.EqualTo(post.userId));
Assert.That(result.HttpResponseMessage.StatusCode, Is.EqualTo(HttpStatusCode.OK));