[CSHARP-1926] Registered Convention Not Applied Created: 21/Feb/17 Updated: 05/Apr/19 Resolved: 13/Oct/17 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | None |
| Affects Version/s: | 2.4 |
| Fix Version/s: | None |
| Type: | Task | Priority: | Major - P3 |
| Reporter: | Michael Georghion | Assignee: | Robert Stam |
| Resolution: | Done | Votes: | 0 |
| Labels: | question | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Description |
|
I have created a Convention in my Asp.Net MVC application to map between using a string Id property in my classes and a BsonId in the database. I register this convention at startup via the Application_Start method in my Global.asax: ConventionRegistry.Register("Custom Conventions", new ConventionPack { new StringObjectIdConvention()}, t => t.FullName.StartsWith("MyProjectName.")); The convention works as expected but when deployed to server it will fail to apply after some amount of time (the error will occur when retrieving data from the database and I will get an "System.FormatException: An error occurred while deserializing the Id property of class "ClassName": Cannot deserialize a 'String' from BsonType 'ObjectId'".). This can sometimes be immediately after deployment or after a seemingly random amount of time. In order to get the convention to work again I have to restart the App Pool. Is there anything I am doing wrong that could cause this behavior? Is there any way I can check if the convention is still registered when the error occurs? As an aside, I am also using Hangfire with Mongo as it's storage in the same web application (using the official Hangfire Mongo driver). Although my registered convention should not apply to to the Hangfire storage (since I apply the filter when registering) is there any chance that this could somehow be interfering? When Hangfire has been disabled I have not noticed the error. |
| Comments |
| Comment by Robert Stam [ 02/Mar/17 ] |
|
I'm not sure what's going on. A stack trace of any exception you are getting might provide helpful information. You want to make sure that your custom StringObjectIdConvention only applies to string fields that hold an ObjectId, not to all string fields. You also want to make sure that your convention is registered very early in your application, certainly before any database operations are performed that might cause some automatic serialization configuration to occur before your convention has been registered. I'm unfamiliar with Hangfire, but according to its Nuget information it depends on the C# driver. Since it is running in the same process it will share serialization configuration information with your application. Since you check for the type name starting with "MyProjectName." in your convention registration this should not be a problem. |