Skip to content

Instantly share code, notes, and snippets.

@jchannon
jchannon / foo.fs
Last active January 1, 2023 19:30
type System.Int32 with
member x.DisplayWithSuffix() =
let f = x.ToString()
let defaultVal = f + "th"
let mutable result = ""
result <- if f.EndsWith("11") then f + "th" else defaultVal
result <- if f.EndsWith("12") then f + "th" else defaultVal
result <- if f.EndsWith("13") then f + "th" else defaultVal
result <- if f.EndsWith("1") then f + "st" else defaultVal
@jchannon
jchannon / instructions.md
Last active March 31, 2021 18:13
Install .NET Core Side By Side on Linux

dotnet-install

Add the below alias to be used in your terminal

alias dni='curl https://dotnet.microsoft.com/download/dotnet-core/scripts/v1/dotnet-install.sh -o ~/.dotnet/dotnet-install.sh -s | chmod +x ~/.dotnet/dotnet-install.sh | ~/.dotnet/dotnet-install.sh'

Execute the below command to see possible options

dni --help

@jchannon
jchannon / gist:1931579
Created February 28, 2012 09:38
Thread Safety
for (int i = 0; i < 100; i++)
{
ManualResetEvent allGo = new ManualResetEvent(false);
var model = new MyClass();
object starter = new object();
int waiting = 20;
int failures = 0;
Exception firstException = null;
Thread[] threads = new Thread[20];
for (int j = 0; j < 10; j++)
type LogLevel =
| Error
| Warning
| Info
let log (level:LogLevel) message = // LogLevel -> string -> unit
printfn "[%A]: %s" level message
()
log Error "Curried function"
-- Message #0
-----------------------------------------------------------------
ETW collector internal error
ETW Collector error
[location] = c:\build agent\work\9f8acdb7e480c131\profiler\kernel\windows\native\solution\core\src\bridges\etw\etw_bridge.cpp(101)
[function] = enum jb::profiler::etw_bridge::loop_executor::state __cdecl jb::profiler::etw_bridge::poll_etw_state(void)
[hresult] = adab0000
-----------------------------------------------------------------
@jchannon
jchannon / Window.xaml
Created September 13, 2012 14:50
Infragistics WPF Filtering
<Window x:Class="LanguageCategoryPhrase.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:igDP="http://infragistics.com/DataPresenter" xmlns:igWindows="http://infragistics.com/Windows"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30*"/>
<ColumnDefinition Width="70*"/>
</Grid.ColumnDefinitions>
@jchannon
jchannon / ToDynamic.cs
Created October 17, 2017 19:15 — forked from orhanveli/ToDynamic.cs
Object or Array to Dynamic in C#
public static class Extensions
{
public static dynamic ToDynamic(this object value)
{
if (value.IsListOrArray ()) {
var list = new List<ExpandoObject> ();
IEnumerable enumerable = value as IEnumerable;
foreach (object o in enumerable) {
list.Add (o.ToDynamic ());
@jchannon
jchannon / bootstrapper.cs
Last active August 31, 2017 01:11
This will handle Nancy content negotiation on errors so the client receives JSON, XML or HTML based on the client Accept headers
public class CustomBootstrapper : DefaultNancyBootstrapper
{
protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
{
pipelines.OnError += (ctx, exception) =>
{
ctx.Items.Add("OnErrorException", exception);
return null;
};
}
Configuration Result:
[Success] Name MyApp
[Success] Description MyApp FrontEnd
[Success] ServiceName MyApp
Topshelf v3.2.150.0, .NET Framework v4.0.30319.19448
Connected to Proxy
The MyApp service is now running, press Control+C to exit.
Running a http server on https://10.240.205.4:443
Topshelf.Hosts.ConsoleRunHost Critical: 0 : The service threw an unhandled exception, System.NullReferenceException: Object reference not set to an instance of an object.
at System.Net.HttpListener.EndGetContext(IAsyncResult asyncResult)
public static class HttpClientExtensions
{
public static T Get<T>(this HttpClient client, string url)
{
var response = client.GetAsync(url).Result;
var content = response.Content.ReadAsStringAsync().Result;
return JsonConvert.DeserializeObject<T>(content);
}
public static HttpResponseMessage PostAsJson(this HttpClient client, string url)