On Thu, 10 Nov 2016 09:17:15 GMT, Sönke Ludwig wrote:

On Wed, 02 Nov 2016 00:55:55 GMT, Uiy Uiy wrote:

If I try to connect to a mongoDB in a static this, it fails.

I suppose that vibe.d has not initialized the mongoDB driver.

It simplifies my code to initialize module level global database variables in static this but this is non-compliant with vibe.d. While I could spawn a thread and spin for a bit until vibe.d has run, this is not the optimal solution.

MongoClient client;
MongoDatabase db;
MongoCollection collection;
static this()
{
	client = connectMongoDB("127.0.0.1");
	db = client.getDatabase("Test");	
	collection = client.getCollection("Test.test");
}

Any ideas?

I just tried this locally and it worked for me for the latest master version of vibe.d. Which version are you using and what error exactly occurs?

latest all(non-beta).

I used this in a separate module, so that might be the issue. I think it is simply the order of execution of the static this.

The errors are

First-chance exception: object.Exception Failed to connect to host 127.0.0.1:27017: Connection refused [WSAECONNREFUSED ] at vibe\core\drivers\libevent2.d(326)

First-chance exception: vibe.db.mongo.connection.MongoDriverException Failed to connect to MongoDB server at 127.0.0.1:27017. at vibe\db\mongo\connection.d(169)

First-chance exception: core.demangle.Demangle.ParseException

If I simply initialize the variables in the vibe.d's static this function, everything works(copy and paste the init code above).

Is there a way to pass a function to be executed by vibe.d after it is done initializing but before normal execution?