c# - SQL CLR for mongodb throwing 'MongoDB.Bson.Serialization.BsonSerializer' exception -


i'm using c# create sql clr procedure basic operations on mongodb contents of collection, inserting document etc. i'm getting the type initializer 'mongodb.bson.serialization.bsonserializer' threw exception.

any ideas ??

the exact same code c# directly works fine.

i'm using c# driver mongo version 1.10, .net framework 3.5, microsoft sql server 2008 (rtm).

below may find sample of stack trace:

failed delete collection db error :the type initializer 'mongodb.bson.serialization.bsonserializer' threw exception. @ mongodb.bson.serialization.bsonserializer.lookupserializer(type type) @ mongodb.driver.mongodatabase.runcommandas[tcommandresult](imongocommand command) @ mongodb.driver.mongodatabase.dropcollection(string collectionname) @ mongo.test()

getting collections db failed collection database error :the type initializer 'mongodb.bson.serialization.bsonserializer' threw exception. @ mongodb.bson.serialization.bsonserializer.lookupserializer(type type) @ mongodb.driver.mongocollection.runcommandas[tcommandresult](imongocommand command, readpreference readpreference) @ mongodb.driver.mongocollection.count(countargs args) @ mongo.test()

inserting document collection ... failed insert collection of db test_blobs error :unable connect server xx.local:27017: type initializer 'mongodb.bson.serialization.bsonserializer' threw exception.. @ mongodb.driver.internal.directmongoserverproxy.connect(timespan timeout, readpreference readpreference) @ mongodb.driver.internal.directmongoserverproxy.chooseserverinstance(readpreference readpreference) @ mongodb.driver.mongoserver.acquireconnection(readpreference readpreference) @ mongodb.driver.mongocollection.insertbatch(type nominaltype, ienumerable documents, mongoinsertoptions options) @ mongodb.driver.mongocollection.insert(type nominaltype, object document, mongoinsertoptions options) @ mongodb.driver.mongocollection.insert[tnominaltype](tnominaltype document) @ mongo.test()

it seems issue, or @ least part of it, starting in 2.x series of .net driver mongodb, target framework version changed 4.5.

so there 2 possibilities:

  1. if need working in sql server 2008 or 2008 r2, can grab source code 1.10 version of .net driver mongodb, found at:
    https://github.com/mongodb/mongo-csharp-driver/tree/v1.10.0
    last version use .net framework version 3.5.

  2. since seems 2.x series of .net driver mongodb work in newer versions of sql server (2012 , newer, assume), have option of:

    • installing sql server 2014 (or 2016) express edition (the free version)
    • loading 2.x version of .net driver mongodb onto express edition instance
    • and setting linked server sql server 2008 instance sql server 201[2,4,6] express edition, , hence use express edition proxy

Comments