Configure RavenDB versioning via code

≯℡__Kan透↙ 提交于 2019-12-01 11:27:41

That code is good, but it just creates the configuration information that the Versioning Bundle will look for. It doesn't actually enable the bundle.

For any named bundle, you enable it by including the name in the Raven/ActiveBundles setting, which is a semicolon-delimited list of bundle names.

By "named" bundles, I mean those that export a "Bundle" name using the [ExportMetadata] attribute. All of the built-in bundles do this. (You can see that the Versioning Bundle exports the name "Versioning" if you dig into the source code of one of its triggers).

If a bundle is unnamed then it is always enabled, as long as it exists either within the RavenDB server assemblies, or in a separate assembly in the \plugins folder.

It looks like the documentation needs updating, as it still says to place the Raven.Bundles.Versioning.dll assembly in the plugins folder. That no longer exists, as it was moved into the main RavenDB server assemblies in 2.0. So for this particular bundle, just editing the settings will be sufficient.

The settings for a named tenant database are kept in the system database in a document called Raven/Databases/<YourDatabaseName>. Simply edit this document once, and the bundle is activated. Here is an extension method that will do that for you:

public static void ActivateBundle(this IDocumentStore documentStore, string bundleName, string databaseName)
{
    using (var session = documentStore.OpenSession())
    {
        var databaseDocument = session.Load<DatabaseDocument>("Raven/Databases/" + databaseName);

        var settings = databaseDocument.Settings;
        var activeBundles = settings.ContainsKey(Constants.ActiveBundles) ? settings[Constants.ActiveBundles] : null;
        if (string.IsNullOrEmpty(activeBundles))
            settings[Constants.ActiveBundles] = bundleName;
        else if (!activeBundles.Split(';').Contains(bundleName, StringComparer.OrdinalIgnoreCase))
            settings[Constants.ActiveBundles] = activeBundles + ";" + bundleName;

        session.SaveChanges();
    }
}

Using the above method, you can simply call:

documentStore.ActivateBundle("Versioning", "YourDatabaseName");

If you are running an embedded-mode database, there are no named tenant databases, so the procedure is a bit different. You can put settings into your own app.config file, or you can manipulate the documentStore.Configuration.Settings dictionary before your existing call to documentStore.Initialize(). Here is a revised version of the extension method that will work on an embedded database:

public static void ActivateBundle(this EmbeddableDocumentStore documentStore, string bundleName)
{
    var settings = documentStore.Configuration.Settings;
    var activeBundles = settings[Constants.ActiveBundles];
    if (string.IsNullOrEmpty(activeBundles))
        settings[Constants.ActiveBundles] = bundleName;
    else if (!activeBundles.Split(';').Contains(bundleName, StringComparer.OrdinalIgnoreCase))
        settings[Constants.ActiveBundles] = activeBundles + ";" + bundleName;
}

Using this method, you can simply do this:

documentStore.ActivateBundle("Versioning");
documentStore.Initialize();
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!