Domanda

I configure my document store in the following way:

store = new DocumentStore { Url = serverUrl };
store.Initialize();

I like to know how I can make sure prior or post initialization but before opening a session whether the client is connected to the server. I did not startup the server and I could still initialize the store, not sure why or whether it creates by default an embedded db if it cannot find the server under specified url. Any idea how to check that the connection is established between client and server?

È stato utile?

Soluzione

Initialization does not actually open a connection. The RavenDB client opens and closes connections as it needs to.

It will not revert to an embedded database. You have to explicitly use an EmbeddableDocumentStore if you want an embedded database instance.

If you want to check yourself if the server is up, you can just do something and see if it fails. Probably the easiest thing you could do is to try to get the build number of the RavenDB server. This can be done using documentStore.AsyncDatabaseCommands.GetBuildNumberAsync().

Here are some extension methods that will help make it even easier. Put these in a static class:

public static bool TryGetServerVersion(this IDocumentStore documentStore, out BuildNumber buildNumber, int timeoutMilliseconds = 5000)
{
    try
    {
        var task = documentStore.AsyncDatabaseCommands.GetBuildNumberAsync();
        var success = task.Wait(timeoutMilliseconds);
        buildNumber = task.Result;
        return success;
    }
    catch
    {
        buildNumber = null;
        return false;
    }
}

public static bool IsServerOnline(this IDocumentStore documentStore, int timeoutMilliseconds = 5000)
{
    BuildNumber buildNumber;
    return documentStore.TryGetServerVersion(out buildNumber, timeoutMilliseconds);
}

Then you can use them like this:

var online = documentStore.IsServerOnline();

Or like this:

BuildNumber buildNumber;
var online = documentStore.TryGetServerVersion(out buildNumber);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top