Friday 2 November 2012

Interrogating NServiceBus Saga data stored in RavenDB

These days, NServiceBus stores its data in RavenDB, including any saga data. Sometimes it is useful to interrogate the saga data to report on running processes.

Querying the saga data stored in NServiceBus is not as simple as plain RavenDB Document Store client, you need to supply conventions to be able to pick up the saga data.

An example is as follows:

    using System;
    using System.Linq;
 
    using NServiceBus.Persistence.Raven;
 
    using Raven.Client;
    using Raven.Client.Document;
 
    public class Example
    {
        public void GetSagaDataFromRavenDB()
        {
            using (IDocumentStore documentStore = new DocumentStore
                {
                    ConnectionStringName = "MyConnectionString",
                    ResourceManagerId = Guid.Parse("C6687DB2-764C-46A4-A3C5-15A3BA22A01A"),
                    Conventions = new DocumentConvention
                        {
                            FindTypeTagName = new RavenConventions().FindTypeTagName
                        }
                })
            {
                documentStore.Initialize();
                using (IDocumentSession documentSession = documentStore.OpenSession())
                {
                    MySagaData[] mySagaDatas =
                        (from mySagaData in documentSession.Query<MySagaData>()
                         select mySagaData).ToArray();
                    foreach (MySagaData mySagaData in mySagaDatas)
                    {
                        Console.WriteLine(mySagaData.SomeProperty);
                    }
                }
            }
        }
    }

The GUID is specific (you need to supply that one) and the "RavenConventions" come from the NServiceBus persistence API.

And that is it.

0 comments:

About Me