Skip to content

Instantly share code, notes, and snippets.

@clemensv
Created February 16, 2013 17:37
Show Gist options
  • Save clemensv/4967845 to your computer and use it in GitHub Desktop.
Save clemensv/4967845 to your computer and use it in GitHub Desktop.
Versatile way to wrap, mock and partially override message senders in SB
using Microsoft.ServiceBus.Messaging;
using System;
using System.Collections.Generic;
namespace MockMessageSender
{
public class MutableMessageSender
{
public delegate void _Send(BrokeredMessage message);
public delegate void _SendBatch(IEnumerable<BrokeredMessage> message);
public delegate IAsyncResult _BeginSend(BrokeredMessage message, AsyncCallback callback, object state);
public delegate void _EndSend(IAsyncResult result);
public delegate IAsyncResult _BeginSendBatch(IEnumerable<BrokeredMessage> message, AsyncCallback callback, object state);
public delegate void _EndSendBatch(IAsyncResult result);
public _Send Send;
public _SendBatch SendBatch;
public _BeginSend BeginSend;
public _EndSend EndSend;
public _BeginSendBatch BeginSendBatch;
public _EndSendBatch EndSendBatch;
public MutableMessageSender(MessageSender sender)
{
this.Send = sender.Send;
this.SendBatch = sender.SendBatch;
this.BeginSend = sender.BeginSend;
this.EndSend = sender.EndSend;
this.BeginSendBatch = sender.BeginSendBatch;
this.EndSendBatch = sender.EndSendBatch;
}
public MutableMessageSender(QueueClient sender)
{
this.Send = sender.Send;
this.SendBatch = sender.SendBatch;
this.BeginSend = sender.BeginSend;
this.EndSend = sender.EndSend;
this.BeginSendBatch = sender.BeginSendBatch;
this.EndSendBatch = sender.EndSendBatch;
}
public MutableMessageSender(TopicClient sender)
{
this.Send = sender.Send;
this.SendBatch = sender.SendBatch;
this.BeginSend = sender.BeginSend;
this.EndSend = sender.EndSend;
this.BeginSendBatch = sender.BeginSendBatch;
this.EndSendBatch = sender.EndSendBatch;
}
public MutableMessageSender()
{
this.Send = message => { };
this.SendBatch = messages => { };
/* too lazy to import CompletedAsyncResult for the async variants, so commented out:
this.BeginSend = (message, callback, state) => { return new CompletedAsyncResult(callback, state); }
this.EndSend = result => { ((CompletedAsyncResult) result).End() };
this.BeginSendBatch = (messages, callback, state) => { return new CompletedAsyncResult(callback, state); }
this.EndSendBatch = result => { ((CompletedAsyncResult) result).End() };
*/
}
}
class Program
{
static void Main(string[] args)
{
var client = QueueClient.CreateFromConnectionString("...", "test");
var sender = new MutableMessageSender(client);
sender.Send(new BrokeredMessage() { Label = "Wah!" });
sender = new MutableMessageSender(client)
{
Send = (b) => { Console.WriteLine(b.Label); client.Send(b); }
};
sender.Send(new BrokeredMessage() { Label = "Baz!" });
var nopSender = new MutableMessageSender();
sender.Send(new BrokeredMessage() { Label = "Boo!" });
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment