Introducing ScriptCs.Rebus

In a recent blog post I introduced the reader to the world of scripting with C# using ScriptCs.  I really love the idea of encapsulating boiler-plate code into a script pack, and be able to consume larger frameworks from simple scripts. One of my favorite frameworks is Rebus. Rebus is this lean and very simple service bus. It is founded and coordinated by my good friend Mogens Heller Grabe.


Well, why not? Rebus is a simple, yet powerful framework for messaging, it is obvious for me to make Rebus the messaging engine for ScriptCs. On the current work project we use Rebus extensively, and to provide some sample messages and for testing purposes, we’ve so far used small console applications. It is my intend to switch over and use ScriptCs.Rebus from now on. With scripting possibilities for messaging I can imagine larger test scenarios, and stress-testing.

Basic Usage from REPL

I’d refer to the readme on GitHub for installation guidelines.

To enter the REPL, run scriptcs. Send a message of type string to MyMessageQueue:

    .Send<string>("My first scripted message!")

Now open another command prompt or use the same, and enter the following to receive the message of type string from MyMessageQueue:

    .Receive<string>(x => Console.WriteLine(x))

The command prompt should output the following:

From MyMessageQueue > My first scripted message!

Basic Usage from Script

The examples from above apply to scripts. Put the send and receive code into two .csx files, lets call them send.csx and receive.csx, and execute them by typing the following from a command prompt:

scriptcs send.csx and

scriptcs receive.csx

In the \samples folder there is some samples to get you started.

Advanced Usage

At this point you should be able to apply these more advanced usages to both the REPL and from scripts.

Custom Types

It is possible to use custom types in your messages. Consider the following class:

public class Message { public string Content { get; set; } }

You enter this class definition in the REPL or create a message.csx script file. If defined in a script file, load this class definition into your script by using #load keyword,

#load "message.csx"

Now you can send and receive messages of this type by using the following syntax:

    .Send<Message>(new Message {Content = "Hello from custom type!"})


    .Receive<Messge>(x => Console.WriteLine(x.Content))


This should output From MyMessageQueue > Hello from custom type.

Multiple Handlers

In some cases you might be expecting to handle different kinds of messages. This can be achieved by adding multiple receive commands fluently:

    .Receive<string>(x => Console.WriteLine(x))
    .Receive<Message>(x => Console.WriteLine(x.Content))

Applying multiple handlers of the same type, will give the possibility to handle message of the same type in different ways.

Future Directions

This is only the first revision of ScriptCs.Rebus, I’ll continue to explore new possibilities with building messaging infrastructure using scripts. I’ve got RabbitMQ and Azure Service Bus aligned in the pipeline.

Get out there and try it out, throw me a comment or a pull request on GitHub.

comments powered by Disqus