Skip to content

Instantly share code, notes, and snippets.

Avatar

Rick Strahl RickStrahl

View GitHub Profile
@RickStrahl
RickStrahl / HttpClientPerfTestMinimal.cs
Created Oct 12, 2021
Simple test of HTTP Client under load with many simultaneous requests. For me this produces about 15000 requests in 20 seconds which is nearly on par with what the old HttpWebRequest client I used produced.
View HttpClientPerfTestMinimal.cs
[TestClass]
public class HttpClientPerfTests
{
private string testUrl = "https://localhost:5001/api/artist/33";
private int counter = 0;
private bool cancel = false;
private HttpClient GetHttpClient(bool force = false)
{
if (!force && _httpClient != null) return _httpClient;
@RickStrahl
RickStrahl / MinimalApiAspNetStartup.cs
Last active Oct 11, 2021
Minimal API Startup in 6.0 for ASP.NET Core application - Auto-Refresh not working for Razor
View MinimalApiAspNetStartup.cs
using LicensingService.Configuration;
using Microsoft.AspNetCore.Authentication.Cookies;
using Newtonsoft.Json.Serialization;
using Westwind.Licensing;
using Westwind.Utilities.Data;
var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;
var configuration = builder.Configuration;
var host = builder.Host;
@RickStrahl
RickStrahl / RunVsCodeFromDotnet.cs
Last active Oct 11, 2021
Demonstrates different ways to launch VS Code from C# code on Windows
View RunVsCodeFromDotnet.cs
void Main()
{
string file = @"C:\temp\test.md";
OpenFileInTextEditorRegistry(file);
//OpenEditorWithShellExecute(file);
//OpenWithProcessStartFailed(file);
}
@RickStrahl
RickStrahl / file.cs
Created Jun 27, 2021
This is a test
View file.cs
int x = 1;
@RickStrahl
RickStrahl / BitmapSourceToBitmap.cs
Last active Oct 11, 2021
BitmapSource To Bitmap Conversion
View BitmapSourceToBitmap.cs
/// <summary>
/// Converts a bitmap source to a bitmap
/// Make sure to dispose the bitmap
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static Bitmap BitmapSourceToBitmap(BitmapSource source)
{
if (source == null)
return null;
@RickStrahl
RickStrahl / ColorConsole.cs
Last active Sep 24, 2021
Color Console - a simple class to add color to .NET Console commands more easily.
View ColorConsole.cs
using System;
using System.Text;
using System.Text.RegularExpressions;
namespace MainColorConsole
{
class Program
{
static void Main(string[] args)
{
@RickStrahl
RickStrahl / Bash-ShellExecute.cs
Last active May 19, 2020
Trying to run `bash -c ""` command with Process.Start/ShellExecute
View Bash-ShellExecute.cs
// works in LinqPad
void Main()
{
ExecuteCommandLine("wt"); // works - this is resolved
ExecuteCommandLine("bash"); // The system cannot find the file specified
// actual command line I'm trying to fire
ExecuteCommandLine("bash -c \"cd /mnt/c/projects/Test/jekyll/help; bundle exec jekyll serve\"");
}
@RickStrahl
RickStrahl / SimpleWebSocketServer-Usage.cs
Last active Oct 10, 2021
Simple Notification Only C# WebSocket Server
View SimpleWebSocketServer-Usage.cs
if (WebSocketServer == null)
{
WebSocketServer = new MarkdownMonster.WebSockets.WebSocketServer();
WebSocketServer.StartServer();
WebSocketServer.OnBinaryMessage = (bytes) =>
{
App.CommandArgs = new[] {"untitled.base64," + Convert.ToBase64String(bytes)};
mmApp.Model.Window.Dispatcher.InvokeAsync(() => mmApp.Model.Window.OpenFilesFromCommandLine());
};
}
@RickStrahl
RickStrahl / StatusDisplay.js
Last active Oct 10, 2021
Drop in JavaScript `status()` function that can be used from code to display debug status message on the bottom of the viewport for scenarios where console.log() is not available.
View StatusDisplay.js
var statusTimeout = null;
var statusDefaultTimeout = 10000;
/*
* Generic Status Message for the bottom of the screen. Can be used to render debug output
* into the editor. Double click status bar to clear for appended output.
* status('Started');
* status('updated',true, 5000);
*/
function status(msg, append, timeout) {
@RickStrahl
RickStrahl / LegacyHostEnvironment.md
Last active Feb 25, 2020
Masking IWebHostEnvironment in .NET Core 2.x for operation like 3.x
View LegacyHostEnvironment.md

IWebHostingEnvironment is not available in .NET Core 2.x, but in 3.x it's the recommended way to access the host environment with threat of IHostingEnvironment being removed in the future. This can be a problem in libraries that need to run both on 2.x and 3.x.

The following tries to mask the differences in a multi-targeted .NET Core project by creating a custom IWebHostEnvironment implementations that picks up values from IHostingEnvironment in 2.x.

#if NETCORE2
using Microsoft.Extensions.FileProviders;

namespace Microsoft.AspNetCore.Hosting
{