2 min read
///
/// Gets the document store.
///

/// 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; } ///

/// Creates static indexes on all Raven servers.
///

/// /// 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); } } ///

/// Creates static indexes programmatically on all secondary Raven servers.
///

private static void CreateRavenStaticIndexesOnSecondaryServers(IDocumentStore documentStore) { var connections = GetReplicationConnectionsFromServer(documentStore); Parallel.ForEach(connections, connection => { 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); } } ); } ///

/// Returns a collection of connections for all secondary Raven servers.
///

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 => 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(); } } ///

/// Contains Raven datastore connection values.
///

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

Was this post helpful?