Skip to content

Instantly share code, notes, and snippets.

View GeorgeTsiokos's full-sized avatar

George Tsiokos GeorgeTsiokos

View GitHub Profile
@GeorgeTsiokos
GeorgeTsiokos / TaskAsyncHelper.cs
Created September 9, 2011 22:31
TaskAsyncHelper not needed with Rx
// See Observable.Empty()
public static Task Empty {
get {
// we have to return a new one every time, other wise the task will be disposed
return MakeEmpty();
}
}
// See Observable.Catch()
public static Task Catch(this Task task) {
@GeorgeTsiokos
GeorgeTsiokos / octopus_grant_log_on_as_a_service_right.ps1
Created August 8, 2016 15:23
Octopus - Grant Log On As a Service Right (Pre-deployment PowerShell script)
$identity = $OctopusParameters["Octopus.Action.WindowsService.CustomAccountName"]
$nugetExists = Test-Path "$env:TEMP\nuget.exe"
if ($nugetExists -ne $true) {
Invoke-WebRequest "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" -OutFile "$env:TEMP\nuget.exe"
}
& $env:TEMP\nuget.exe install Carbon -ExcludeVersion -OutputDirectory $env:TEMP
Import-Module $env:TEMP\Carbon\Carbon
Grant-Privilege -Identity $identity -Privilege SeServiceLogonRight
using System;
using System.Reactive.Disposables;
using System.Text;
using System.Threading;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
namespace RabbitAppInsightsHelloWorld
@GeorgeTsiokos
GeorgeTsiokos / WithNamedParameters.cs
Last active November 30, 2016 14:54
Autofac: use constructor parameter names to resolve named dependencies
public static void WithNamedParameters<TImplementer> (this IRegistrationBuilder<TImplementer, ReflectionActivatorData, SingleRegistrationStyle> registrationBuilder) {
var activatorData = registrationBuilder.ActivatorData;
var implementationType = activatorData.ImplementationType;
var constructors = activatorData.ConstructorFinder.FindConstructors (implementationType);
registrationBuilder.OnPreparing (preparingEventArgs => OnPreparing (constructors,
activatorData.ConstructorSelector,
preparingEventArgs));
}
@GeorgeTsiokos
GeorgeTsiokos / SubscribeWithDisposable.cs
Created December 5, 2016 15:57
Rx Subscribe where onNext returns a IDisposable
public static IDisposable Subscribe<TSource> (this IObservable<TSource> sequence, Func<TSource, IDisposable> onNext) {
var disposePreviousValues = sequence.Aggregate (Disposable.Empty,
(disposable, value) => {
using (disposable)
return onNext (value);
});
var disposeLastValue = disposePreviousValues.TakeLast (1).Subscribe (last => last.Dispose ());
return disposeLastValue;
}
@GeorgeTsiokos
GeorgeTsiokos / SynchronizationContextExtensions.cs
Created December 6, 2016 18:05
SynchronizationContextExtensions - Task<T> ExecuteAsync<T>
public static class SynchronizationContextExtensions {
public static Task<T> ExecuteAsync<T> (this SynchronizationContext synchronizationContext, Func<T> factory, CancellationToken cancellationToken = default (CancellationToken)) {
var state = new State<T> (factory,
cancellationToken);
synchronizationContext.Post (ExecuteAsync<T>,
state);
return state.Task;
}
public static class CancellationTokenExtensions {
/// <summary>
/// Creates an observable sequence from a cancellationToken
/// </summary>
/// <param name="cancellationToken">token to observe</param>
/// <param name="throwOperationCanceledException">true to terminate the sequence with an exception instead of emitting a value</param>
/// <returns>an observable sequence that emits a value when the token is fired</returns>
public static IObservable<Unit> ToObservable (this CancellationToken cancellationToken, bool throwOperationCanceledException = false) {
if (!cancellationToken.CanBeCanceled) {
/// <summary>
/// Returns the elements from the source observable sequence only after the predicate returns true (non-inclusive)
/// </summary>
/// <typeparam name="TSource">The type of the elements in the source sequence</typeparam>
/// <param name="source">Source sequence to propagate elements for</param>
/// <param name="predicate">A function to test each source element for a condition</param>
/// <returns>An observable sequence containing the elements of the source sequence starting from the point the predicate is true</returns>
[NotNull]
public static IObservable<TSource> SkipUntil<TSource>([NotNull] this IObservable<TSource> source, [NotNull] Func<TSource, bool> predicate)
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("https://login.live.com");
/// <summary>Filters the elements of an observable sequence based on a nullability.</summary>
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
/// <param name="source">An observable sequence whose elements to filter.</param>
/// <returns>An observable sequence that contains elements from the input sequence that are not <c>null</c>.</returns>
/// <exception cref="T:System.ArgumentNullException"><paramref name="source" /> is null.</exception>
[NotNull]
[ContractAnnotation("source:null => halt")]
public static IObservable<TSource> WhereNotNull<TSource>([NotNull] this IObservable<TSource> source) where TSource : class
{
if (source == null)