Rhino ServiceBus Saga

ayendeI’m a huge fan of Rhino Mocks and have written about it on this blog. Today I read that Ayende has an implementation for a Service Bus. Like nServicebus but “free”, even Udi Dahan talks about it. Now I take a look at the long-running stateful workflow called Saga.

Starting the Saga

The Saga is defined with a generic interface with the type of the state. Implementing the interface adds the properties Id, State and IsCompleted.

public class GroupingSaga : ISaga<GroupSagaState> {
    public GroupSagaState State { get; set; }
    public Guid Id { get; set; }
    public bool IsCompleted { get; set; }
}

One message will be used to start the Saga. When this message implements the ISagaMessage interface the CorrelationId of the message is used to set the Saga Id. Is the message without the interface then the CorrelationId/Saga Id should be returned to the sender in order to continue the same Saga. I’m using the ISagaMessage implementation in my sample.

public class GroupingSaga : InitiatedBy<ProcessFileCommand> {
    public void Consume(ProcessFileCommand message) {
        // logic for starting the Saga
    }
}

After the Saga is started the State (actual the complete Saga) is saved with the SagaPersister. Since I use StructureMap as IoC I make sure the same persister is used else the Saga can never be retrieved. This is done in the ConfigureContainer of the Bootstrapper by defining the persister as a Singleton.

protected override void ConfigureContainer() {
    base.ConfigureContainer();
    Container.Configure(x => {
        x.For(typeof(ISagaPersister<>))
         .Singleton() // use the same persister every time
         .Use(typeof(InMemorySagaPersister<>));
    });
}

Next messages and Saga completion

Now that the Saga has started it can process the next messages. In my sample messages must be collected until a certain time of silence (no messages).

To send a message to the Saga it must contain the Id of the Saga. The ISagaMessage interface takes care of that. On the Saga I must define the Orchestrates interface so that it knows it should handle the message when the Saga has not completed (else the message is discarded).

public class GroupingSaga : Orchestrates<CheckReadyCommand> {
    public void Consume(CheckReadyCommand message) {
        // check LastAction to complete the Saga
    }
}

A Saga is completed when the IsCompleted property is set to True. When the Saga completes it can no longer process messages and the (default) persister will remove it from the storage.

Conclusion

Saving the Saga in memory is good for testing, but insufficient for production, make sure to use something like the RavenDb persister in the References.
Also the Saga implementation of Ayende works good, just like the servicebus in general. What’s missing is the tooling and out-of-the-box production solutions, but you can make and share that yourself. Welcome to the world of open source.

References

rhino-esb-raven a RavenDB Saga persister on github
Learn how to use Rhino Service Bus on Hibernating Rhinos
RhinoServiceBusSaga in my github repositories
BSD-3 license of Rhino ServiceBus

Posted in Development | Tagged , , | Leave a comment

Visual Studio Shims and Fakes

Microsoft Visual Studio 2013 includes Fakes (Premium and above) to isolate existing code. This involves intercepting calls to the framework and rerouting them to Shims. These Shims are created by right clicking a reference and selecting “Add Fakes Assembly”. But sometimes a little manual override is needed to get things working.

[TestMethod]
public void Stub_environment_and_dns() {
   // arrange
   using (ShimsContext.Create()) {
      System.Fakes.ShimEnvironment.MachineNameGet = () => "hello";
      System.Net.Fakes.ShimDns.GetHostName = () => "world";

   // act
      var result = string.Format("{0} {1}",
         System.Environment.MachineName,
         System.Net.Dns.GetHostName());

   // assert
      Assert.AreEqual("hello world", result);
   }
}

To get the above code to work edit the mscorlib.fakes and System.fakes files. We need to instruct the generator to create fakes for System.Net.Dns and System.Environment.

<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
   <Assembly Name="System" Version="4.0.0.0" />
   <ShimGeneration>
      <Add FullName="System.Net.Dns"/>
   </ShimGeneration>
</Fakes>
<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
   <Assembly Name="mscorlib" Version="4.0.0.0"/>
   <ShimGeneration>
      <Add FullName="System.Environment"/>
   </ShimGeneration>
</Fakes>

Still the build output contains warnings:

1>mscorlib.fakes : warning : Some fakes could not be generated. For complete details, set Diagnostic attribute of the Fakes element in this file to ‘true’ and rebuild the project.
1>System.fakes : warning : Some fakes could not be generated. For complete details, set Diagnostic attribute of the Fakes element in this file to ‘true’ and rebuild the project.

This makes me reserved in using this for my projects.

Posted in Development | Tagged , | 2 Comments

Week roundup

Last week recap and links:

Image courtesy of kanate / FreeDigitalPhotos.net

Image courtesy of kanate / FreeDigitalPhotos.net

What are your best reads this week? Leave them in the comments below.

Posted in Uncategorized | Tagged , , , | Leave a comment

Windows 8.1 Reliability Monitor

Thanks to Scott Hanselman’s tool list I started using the Reliability Monitor. It is located in Control Panel\All Control Panel Items\Action Center\Reliability Monitor. Below is a screenshot of the report on my computer.

reliability_report

Before the only days my computer had no critical events (red icon) was in the weekends. When the computer would not be turned on. There was definitely something wrong with my machine.

After fixing the issue with the video driver (on a Wednesday) my machine stopped producing critical events. Not that I notice any difference in performance, but the feeling is great!

Posted in Tooling | Tagged | Leave a comment

SQLite WARNING: Type mapping failed

The log of our product started showing SQLite warnings. We ignored them to focus on the release. Now it was time to repro and fix the warnings.

After reading the verbose log we found the issue. Unknown datatypes are allowed when creating SQLite database, but log a warning when the check database routine would call GetSchema.

WARNING: Type mapping failed, returning default type Object for name “LARGEINT”.
WARNING: Type mapping failed, returning default name “” for type Object.

See the repro code below. System.Data.SQLite can be downloaded from here.

static void Main(string[] args) {
    // Trace to see SQLite warning
    Trace.Listeners.Add(new ConsoleTraceListener());
    // Use in-memory database
    var connectionString = "Data Source=:memory:;Version=3;";
    using(var connection = new SQLiteConnection(connectionString)) {
        connection.Open();
        // Create table with unknown datatype column
        using (var createTableOne = connection.CreateCommand()) {
            createTableOne.CommandText = 
                "CREATE TABLE One (Id LARGEINT NOT NULL);";
            createTableOne.ExecuteNonQuery();
        }
        // Table is created, no exceptions, only warning on GetSchema
        var schema = connection.GetSchema("Columns", null);
        var result = schema.Select("TABLE_NAME = 'One'");
        if (result.Length > 0) Console.WriteLine("One exists");
        else Console.WriteLine("One not exists");
    }
    Console.ReadLine();
}

We edited the create database scripts to use known SQLite types and the issue was resolved.

Posted in Development | Tagged , | Leave a comment