RavenDB, programmatically check if a server instance is running

I set up my document repository as follows:

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

I like to know how I can make sure the previous or post-initialization , but before the session opens, is the client connected to the server. I did not start the server, and I could still initialize the repository, I don’t know why, or whether it creates a default built-in db if it cannot find the server under the specified URL. Any idea how to check if a connection is established between the client and server?

+5
source share
1 answer

Initialization does not actually open the connection. The RavenDB client opens and closes connections as it sees fit.

. EmbeddableDocumentStore, .

, , - , . , , , RavenDB. documentStore.AsyncDatabaseCommands.GetBuildNumberAsync().

, . :

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

:

var online = documentStore.IsServerOnline();

:

BuildNumber buildNumber;
var online = documentStore.TryGetServerVersion(out buildNumber);
+8

All Articles