So, I was wondering if there is a way to load and use a .NET library on the server. I ask this because I want to set up a connection to a MongoDB server to store some data.
What I’ve tried:
I’ve downloaded the driver files from here and have put them into a folder on the server. I’ve then tried the following:
local reflection = clr.System.Reflection
-- Load the mongodb drivers
reflection.Assembly.LoadFrom("resources/test/lib/mongo/MongoDB.Driver.dll")
client = clr.MongoDB.Driver.MongoClient( connectionString )
However, I get a “Object is nil” error on the last line. I’ve managed to figure out that “MongoClient” isn’t a class that can be called but, the documentation differs.
Any help would be appreciated
Note: I can use the MySQL library that came with essential so, I figure that it must be something to do with the library itself. Does it need a specific function or does it need to be compiled for a specific system?
Is there anything in particular that needs doing in the Lua file called through dofile (at the minute i’m just printing a couple of messages)? I’ve tried creating the MongoClient in it but, I get the same error.
I’ve also tried to create the client by doing the code below (as pointed out at the link provided)
const MongoClient typeof clr.MongoDB.Driver.MongoClient -- Also tried just "MongoDB.Driver.MongoClient"
print("MongoClient: " .. MongoClient)
local client = MongoClient(connectionString)
print("created client")
However, I end up with the same error (Object is nil):
The MongoDB client library is strange - it appears to depend on System.Runtime.InteropServices.RuntimeInformation.dll and fails to load it, even in the NeoLuaCmd test console, so I can’t see if that loads from there.
Perhaps writing a custom C# wrapper that returns a MongoClient object might help?
Ok. So I wrote a quick wrapper like you suggested and it got a little further (Yey ). I assume the new error is because of my incompetence so, I’d like for some help with it
Wrapper:
using MongoDB.Driver;
namespace Mongo_Wrapper
{
public class Test
{
public static MongoClient GetClient(string connString)
{
return new MongoClient(connString);
}
}
}
Client:
local client = clr.Mongo_Wrapper.Test:GetClient("mongodb://localhost:27017")
That’s a typical error due to conflicting .NET Standard library dependencies with the classical .NET Framework. Perhaps finding the right assembly from NuGet for System.Runtime.InteropServices.RuntimeInformation and placing it in the folder with the server EXE would help.