Skip to content

Instantly share code, notes, and snippets.

View clemensv's full-sized avatar
🏠
Working from home

Clemens Vasters clemensv

🏠
Working from home
View GitHub Profile
@clemensv
clemensv / gist:10841337
Last active August 29, 2015 13:59
Cross-AppDomain Marshaling friendly TPL wrappers for APM
//FromAsync variant that can be used with APM across M<arshaling boundaries (you make your own TArgX overloads, please)
static class MarshalTask
{
public static Task<TResult> FromAsync<TArg1, TArg2, TResult>(Func<TArg1, TArg2, AsyncCallback, object, IAsyncResult> beginFunc, Func<IAsyncResult, TResult> endFunc, TArg1 arg1, TArg2 arg2)
{
var cp = new Completer<TResult>(endFunc);
var tcs = new TaskCompletionSource<TResult>(cp);
cp.TaskCompletionSource = tcs;
@clemensv
clemensv / gist:dd1d629beec9d38b747d
Created August 27, 2014 13:51
SAS in namespace root
NamespaceManager nsm = NamespaceManager.CreateFromConnectionString(serviceBusConnectionString);
string name = nsm.Address.Host.Substring(0, nsm.Address.Host.IndexOf(".", System.StringComparison.Ordinal));
var sendKey = SharedAccessAuthorizationRule.GenerateRandomKey();
var listenKey = SharedAccessAuthorizationRule.GenerateRandomKey();
var manageKey = SharedAccessAuthorizationRule.GenerateRandomKey();
CreateSharedAccessRuleOnNamespaceRoot(name, "send", sendKey, AccessRights.Send, subscriptionId, publisherCertificate);
CreateSharedAccessRuleOnNamespaceRoot(name, "manage", manageKey, AccessRights.Manage|AccessRights.Send|AccessRights.Listen, subscriptionId, publisherCertificate);
CreateSharedAccessRuleOnNamespaceRoot(name, "listen", listenKey, AccessRights.Listen, subscriptionId, publisherCertificate);
@clemensv
clemensv / gist:639f832e5151482054f8
Last active August 29, 2015 14:06
"alias" classes in C# ?!
Thought experiment:
Listened to eight .NET Rocks episodes yesterday while driving. One episode was about C# 6.0 with Bill Wagner
(http://www.dotnetrocks.com/default.aspx?showNum=1029), another about DDD with Steve Smith and Julie Lerman
(http://www.dotnetrocks.com/default.aspx?showNum=1023) where they specifically also discussed DDD's notion of
an anti-corruption layer, which aims to provide a neutral zone between different domains. The combination of
the two episodes gave me the following idea which I'll jot down here quickly.
Don't go "you dont do that in DDD, because .." since that's not the point. The point is efficiency and avoiding
data copies. Also, I'm not planning on doing anything further with it, so if anyone wants to take this somewhere,
@clemensv
clemensv / gist:2589551
Created May 3, 2012 21:12
I actually wasn't kidding about entityless HTTP calls
var wr = WebRequest.Create("http://localhost:39472/Provisioning.svc/setup");
wr.Method = "POST";
wr.Headers.Add("P-DeviceId", this.deviceId);
using (var wq = (HttpWebResponse)wr.GetResponse())
{
if (wq.StatusCode == HttpStatusCode.OK)
{
settings.DeviceAccount = wq.Headers["P-DeviceAccount"];
settings.DeviceKey = wq.Headers["P-DeviceKey"];
settings.DeviceSubscriptionUri = new Uri(wq.Headers["P-DeviceSubscriptionUri"]);
@clemensv
clemensv / ProcessingJobsController.cs
Created May 10, 2012 19:37
Message Correlation - Web Role Side
namespace CorrelationSite.Controllers
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web.Mvc;
using CorrelationSite.Models;
using Microsoft.ServiceBus.Messaging;
@clemensv
clemensv / CorrelationWorker.cs
Created May 10, 2012 19:34
Message Correlation - Worker Role Side
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BackgroundWorker
{
using System.Threading;
using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;
@clemensv
clemensv / gist:3540372
Created August 30, 2012 20:40
Yes, a goto courtesy of async/await's scoping issues with "catch".
// The scoping model in "catch" doesn't allow for async/await handling because
// catch needs to be able to rethrow without messaging up the stack frame, i.e. "throw;"
// There's obviously an alternate non-goto solution here with a result variable and a
// subsequent switch on that result, but if you code that up, the switch looks like
// a longer, uglier variant of the gotos.
static async Task LoopedReceive(Func<Task<BrokeredMessage>> receiveCallbackAsync,
Func<BrokeredMessage, Task<bool>> messageReceivedCallbackAsync,
CancellationToken token)
{
@clemensv
clemensv / gist:3652025
Created September 6, 2012 06:18
Send with retry
namespace MartVue.Common
{
using System;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using Microsoft.ServiceBus.Messaging;
public static class RobustSendExtension
{
static readonly TaskFactory TaskFactory = new TaskFactory();
@clemensv
clemensv / gist:3817582
Created October 2, 2012 09:04
C# class extensibility pattern
public interface ISomeExtension
{
object Private { get; set; }
}
public static class SomeExtension
{
class State
{
public string data;
@clemensv
clemensv / gist:3853572
Created October 8, 2012 16:56
Writes
namespace Writing
{
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
class Program
{
static void Main(string[] args)