Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1174 Warning: Invalid argument supplied for foreach() in D:\home\site\wwwroot\wp-includes\translations.php on line 1367
/// <summary>
/// Gets the document store.
/// </summary>
/// The document store.
/// 
/// Do this only once per AppDomain load. It's very expensive.
/// 
private static IDocumentStore GetDocumentStore()
{
    // Create the DocumentStore (expensive operation).
    IDocumentStore documentStore = new DocumentStore
    {
        ConnectionStringName = "RavenDB",
        Credentials = System.Net.CredentialCache.DefaultNetworkCredentials // For "trusted connections": see comments at http://ravendb.net/docs/client-api/connecting-to-a-ravendb-datastore
    };

    // Read from and write to all servers.
    documentStore.Conventions.FailoverBehavior = FailoverBehavior.ReadFromAllServers
        | FailoverBehavior.AllowReadsFromSecondariesAndWritesToSecondaries;

    // Initialize the store (must be done before creating indexes)
    documentStore = documentStore.Initialize();

    // Create static indexes
    CreateRavenStaticIndexes(documentStore);

    // Return document store
    return documentStore;
}

/// <summary>
/// Creates static indexes on all Raven servers.
/// </summary>
/// 
/// Static indexes are not replicated in Raven.
/// 
private static void CreateRavenStaticIndexes(IDocumentStore documentStore)
{
    try
    {
        // Create static indexes on primary server
        IndexCreation.CreateIndexes(typeof(AllCommentsIndex).Assembly, documentStore);

        // Create static indexes on secondary servers
        CreateRavenStaticIndexesOnSecondaryServers(documentStore);
    }
    catch (Exception ex)
    {
        var logger = NLog.LogManager.GetCurrentClassLogger();
        logger.FatalException(ex.ToString(), ex);
    }
}

/// <summary>
/// Creates static indexes programmatically on all secondary Raven servers.
/// </summary>
private static void CreateRavenStaticIndexesOnSecondaryServers(IDocumentStore documentStore)
{
    var connections = GetReplicationConnectionsFromServer(documentStore);
    Parallel.ForEach(connections, connection =&gt;
        {
            try
            {
                // Initialize document store for secondary server
                var docStore = new DocumentStore
                { 
                    Url = connection.Url,
                    DefaultDatabase = connection.DefaultDatabase,
                    ApiKey = connection.ApiKey,
                    Credentials = System.Net.CredentialCache.DefaultNetworkCredentials
                }.Initialize();

                // Create static indexes
                IndexCreation.CreateIndexes(typeof(AllCommentsIndex).Assembly, docStore);
            }
            catch (System.Exception ex)
            {
                var logger = NLog.LogManager.GetCurrentClassLogger();
                logger.FatalException(ex.ToString(), ex);
            }
        }
    );
}

/// <summary>
/// Returns a collection of connections for all secondary Raven servers.
/// </summary>
private static IEnumerable GetReplicationConnectionsFromServer(IDocumentStore documentStore)
{
    try
    {
        var connections = new List();
        var rawResult = documentStore.DatabaseCommands.Get("Raven/Replication/Destinations");
        connections.AddRange(rawResult.DataAsJson.SelectToken("Destinations").Values()
                                            .Select(i =&gt; new RavenConnection
                                                {
                                                    Url = i.Value("Url"),
                                                    ApiKey = i.Value("ApiKey"),
                                                    DefaultDatabase = i.Value("Database")
                                                })
                                            .ToList());
        return connections;
    }
    catch (Exception ex)
    {
        var logger = NLog.LogManager.GetCurrentClassLogger();
        logger.FatalException(ex.ToString(), ex);
        return new List();
    }
}

/// <summary>
/// Contains Raven datastore connection values.
/// </summary>
class RavenConnection
{
    public string Url { get; set; }
    public string DefaultDatabase { get; set; }
    public string ApiKey { get; set; }
}

References
- http://ravendb.net/kb/37/static-index-definitions-and-the-replication-bundle