Skip to content

Instantly share code, notes, and snippets.

@bronumski
bronumski / Program.cs
Last active March 15, 2018 20:51
C# Functional Try Catch Finally
public static int Main(string[] args) =>
Try
.Action(() => 0)
.WithCatch<Exception>(ex => 1})
.Finally(CleanUpMethod);
@bronumski
bronumski / CleanFileName.cs
Created March 15, 2018 16:16
Remove invalid chars from a file name using linq
public static string CleanFileName(string fileName)
{
return Path.GetInvalidFileNameChars().Aggregate(fileName, (current, c) => current.Replace(c.ToString(), string.Empty));
}
@bronumski
bronumski / HttpClientExtensions.cs
Last active March 1, 2018 18:25
Change request header for individual requests
public static Task<HttpResponseMessage> GetAsync
(this HttpClient httpClient, string uri, Action<HttpRequestMessage> preAction)
{
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, uri);
preAction(httpRequestMessage);
return httpClient.SendAsync(httpRequestMessage);
}
@bronumski
bronumski / ToDynamic.cs
Created May 23, 2016 11:26
Extension to convert an object to an ExpandoObject
public static class DynamicExtensions
{
public static dynamic ToDynamic(this object value)
{
IDictionary<string, object> expando = new ExpandoObject();
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(value.GetType()))
expando.Add(property.Name, property.GetValue(value));
return expando as ExpandoObject;
@bronumski
bronumski / OctopusSlackIntegration.ps1
Last active May 20, 2016 15:19
Playing with slack integration from Octopus Deploy
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
class HttpApiGuidlinesMiddleware : OwinMiddleware
{
public HttpApiGuidlinesMiddleware(OwinMiddleware next) : base(next) { }
public override async Task Invoke(IOwinContext context)
{
var isApiCall = context.Request.Path.StartsWithSegments(new PathString("/api"));
Stream originalStream = null;
Stream responseBuffer = null;
@bronumski
bronumski / GetAssemblyFileVersion.cs
Created January 8, 2015 19:36
Get Assembly File Version
Assembly assembly = Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
string version = fvi.FileVersion;
@bronumski
bronumski / GenericConverter.cs
Created December 10, 2014 18:52
Basic idea on how to look up a type converter based on a generic type and convert to that value from a string
public TValue Value<TValue>(string value, TValue defaultValue = default(TValue))
{
if (string.IsNullOrEmpty(value)) return defaultValue;
var converter = TypeDescriptor.GetConverter(typeof (TValue));
return (TValue) converter.ConvertFrom(value);
}
@bronumski
bronumski / Publisher.cs
Created September 23, 2014 15:35
RabbitMQ Demo
using System;
using System.Text;
using RabbitMQ.Client;
namespace Publisher
{
class Program
{
static void Main(string[] args)
{
@bronumski
bronumski / UriManager.cs
Created June 19, 2014 11:25
Creates or gets a uri for a service or unique name with a random free port.
static class UriManager
{
private static readonly IDictionary<string, Uri> Uris = new Dictionary<string, Uri>();
public static Uri GetUriForService<TService>()
{
return GetUri(typeof (TService).Name);
}
public static Uri GetUri(string serviceName)