2

I am trying upload a file to Mongo Atlas with C# Mongo Driver. But I not understand how to connect with class MongoServerSettings. I tried this:

private static MongoServerSettings GetMongoConfig()
{
    var credential = MongoCredential.CreateCredential("admin", "XXXX", "XXXX");

    MongoServerAddress[] servers = new MongoServerAddress[3];
    servers[0] = new MongoServerAddress("cluster0-shard-00-00-XXXXX.mongodb.net", 27017);
    servers[1] = new MongoServerAddress("cluster0-shard-00-01-XXXX.mongodb.net", 27017);
    servers[2] = new MongoServerAddress("cluster0-shard-00-02-XXXX.mongodb.net", 27017);

    var ServerSetting = new MongoServerSettings {
        ConnectionMode = ConnectionMode.ReplicaSet,
        UseSsl = true,                
        Servers = servers,
        ReplicaSetName = "Cluster0-shard-0",
        Credentials = new[]{ credential },
    };

    return ServerSetting;
}

public static void SavePDFInvoice(HttpPostedFileBase file,string fileID, string userID, string userOwner)
{
    var Server = new MongoServer(GetMongoConfig());

    var grid = new MongoGridFS(Server, PDFDatabase, new MongoGridFSSettings());
    grid.Upload(file.InputStream, fileID, new MongoGridFSCreateOptions
    {
        Id = fileID,
        Metadata = new BsonDocument {
            { "userID", userID},
            { "userOwner", userOwner},
        } 
    });
}

But, doesn't work.

I can connect with the mongoclient class, but I do not know how to use the MongoGridFS class.

Saravanan Sachi
  • 2,572
  • 5
  • 33
  • 42
  • [From the documentation](http://mongodb.github.io/mongo-csharp-driver/2.4/reference/gridfs/gettingstarted/) you should be using a `GridFSBucket` which takes a provided argument of `IMongoDatabase` which you obtain from connection obtained with `MongoClient`. Seems like you are trying to use a deprecated interface. Instead do what the documentation says. – Neil Lunn Jul 04 '17 at 02:44
  • Adding to that. You appear to be trying to "connect" within the exposed method. Don't do that. Instead persist your connection to MongoDB for the lifecycle of the the application. Then only use the methods to select the current database namespace or collection namespace as appropriate to the method using. Re-instantiating and hanging up database connections "per request" is not a recommended pattern. – Neil Lunn Jul 04 '17 at 02:56
  • Thanks Neil, You're right – Deivid Cristian Nascimento Jul 06 '17 at 01:45

1 Answers1

1

you could check out this link on how to connect to mongodb Atlas using C#

https://docs.atlas.mongodb.com/driver-connection/#c-net-driver-example

From the link:

To connect to an Atlas M0 (Free Tier) cluster, you must use a C#/.Net driver version that supports MongoDB 3.4.

var client = new MongoClient("your clister url");
var database = client.GetDatabase("test");
  • Please post main part of that link info in the answer. In the future that link could be broken and this answer will be useless. Thank you – acostela Jan 09 '18 at 14:53