简体   繁体   中英

MongoDB Serialization() is not supported

Not sure what I am doing and can not find any other references out there about what this error is or even related to.

I get the error message Serialization().Reference is not supported when I try the code below with MondoDB and C#.NET.

I first tried this in my code:

var maxReference = await Events.Find(p => true)
                               .SortByDescending(p => p.Reference)
                               .Project(p => p.Reference)
                               .FirstOrDefault();

When that failed on the FirstOrDefault method, I slowly removed method by method until I had to do this:

var list = await Events.Find(p => true).ToListAsync();
var maxReference = list.Select(p => p.Reference)
                       .OrderByDescending(p => p)
                       .FirstOrDefault();

I was willing to work with this if it was a real limitation, but I ran into it again when I tried to use the ReplaceOneAsync method.

var form = new Event { Reference = maxReference + 1 };
var options = new UpdateOptions { IsUpsert = true };

await Events.ReplaceOneAsync(p => p.Reference == maxReference, form, options);

My POCO is defined as:

[BsonIgnoreExtraElements]
public class Event : IEvent
{
    public Event() {}

    public Event(int reference)
    {
        Reference = reference;
    }

    [BsonId]
    public ObjectId EventID { get; set; }

    [BsonRequired]
    [BsonIgnoreIfDefault]
    public int Reference { get; set; }
}

So it turned out to be in my case that in dependency injection I used interfaces and that is not supported. I guess it has to do with instantiation of the Mongo Documents? Below is the final bootstrapper class. So it appears to ended up as not a Mongo problem as much as a Nancy problem.

I change the line var collection = database.GetCollection<ICollection>("collection"); to var collection = database.GetCollection<Collection>("collection"); .

Notice the ICollection interface to Collection class. And everything worked after that.

Before:

public class Bootstrapper : DefaultNancyBootstrapper
{
    protected override void ConfigureApplicationContainer(TinyIoCContainer container)
    {
        var connectionString = ;
        var databaseName = ;

        var client = new MongoClient("MongoDatabaseURL");
        container.Register(client);

        var database = client.GetDatabase("MongoDatabaseName");
        container.Register(database);

        var collection = database.GetCollection<ICollection>("collection");
        container.Register(collection);
    }
}

After:

public class Bootstrapper : DefaultNancyBootstrapper {
    protected override void ConfigureApplicationContainer(TinyIoCContainer container)
    {
        var connectionString = ;
        var databaseName = ;

        var client = new MongoClient("MongoDatabaseURL");
        container.Register(client);

        var database = client.GetDatabase("MongoDatabaseName");
        container.Register(database);

        var collection = database.GetCollection<Collection>("collection");
        container.Register(collection);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM