This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Text; | |
namespace StringBuilderStream | |
{ | |
public class StringBuilderStream : Stream | |
{ | |
public StringBuilder stringBuilder { get; private set; } | |
public Encoding Encoding { get; private set; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void Main(string[] args) | |
{ | |
try | |
{ | |
const int AesBlockSize = 128; | |
const CipherMode AesMode = CipherMode.CBC; | |
const int AesKeySize = 256; | |
byte[] IV = new byte[] {148, 22, 160, 30, 29, 229, 95, 124, 57, 3, 236, 225, 83, 164, 109, 89}; | |
byte[] key = new byte[] {151, 171, 230, 106, 79, 228, 203, 65, 11, 229, 238, 157, 170, 26, 6, 75, 205, 122, 87, 11, 134, 161, 159, 17, 93, 140, 66, 222, 216, 158, 23, 73 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class ParallelTaskRunnerExtension | |
{ | |
public static void RunTasks(this IEnumerable<Task> tasks, int maxConcurrency, Action<Task> taskComplete = null) | |
{ | |
if (maxConcurrency <= 0) throw new ArgumentException("maxConcurrency must be more than 0."); | |
int taskCount = 0; | |
int nextIndex = 0; | |
var currentTasks = new Task[maxConcurrency]; | |
foreach (var task in tasks) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class TObservable<T> : IObservable<T>, IDisposable | |
{ | |
private readonly IObservable<Task<T>> taskObservable; | |
private IDisposable taskObservableSubscriber; | |
private IObserver<T> observer; | |
private readonly int maxConcurrency; | |
private Task<T>[] currentTasks; | |
private int taskCount = 0; | |
private int nextIndex = 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void GetValueIObservable2() | |
{ | |
var repo = new AccountRepository(); | |
var accounts = repo.GetAccountWithSubs("123123123"); | |
//create a cold observable from IEnumerable... | |
var accountObservable = accounts.ToObservable(); | |
//you can filter an IObservable just like an IEnumerable... | |
//.Where(a => a.AccountNumber.ToString().StartsWith("2")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void button2_Click(object sender, EventArgs e) | |
{ | |
try | |
{ | |
//both methods do basically the same thing. | |
//I recommend the first since it's much simpler. | |
var body = RunSynchronouslyInAnotherTask(GetBody); // method 1 | |
//var body = RunSynchronouslyInThreadPool(GetBody); // method 2 | |
this.textBox1.Text = body; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password))) | |
$dict = @{Authorization=("Basic {0}" -f $base64AuthInfo)} | |
$dict.Add("Accept", "application/json") | |
$response = Invoke-RestMethod -Uri $Url -Method Get -ContentType "application/json" -Headers $dict | |
$response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This works best for cold observables where you want to run Async methods after you subscribe. | |
// I may incorporate this into my QueryRunner....Not sure. | |
using System; | |
using System.Threading.Tasks; | |
namespace AsyncObservable | |
{ | |
public interface IAsyncObserver<in T> | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Note: this was pretty much an acedemic exercise... I don't recommend using this pattern. IObservable would be much better suited. | |
class SomeRepository : IDisposable | |
{ | |
private const string sql = "select * from sometable;"; | |
private const string connString = @"Data Source=LOCALHOST\SQLEXPRESS;Initial Catalog=sandbox;Integrated Security=SSPI;"; | |
public void Dispose() | |
{ | |
if (reader != null) reader.Dispose(); | |
if (connection != null) connection.Dispose(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//boy am I looking forward to read-only classes in c# ver6. | |
public class WebResponseException : Exception | |
{ | |
public readonly HttpStatusCode StatusCode; | |
public readonly string RequestUrl; | |
public readonly string RequestMethod; | |
public readonly Dictionary<string, string> RequestHeaders; | |
public readonly Dictionary<string, string> ResponseHeaders; | |
public readonly string ResponseBody; |
OlderNewer