在IIS网站中清除MongoDB键以解决重复键添加异常
Hey there! Let's work through this duplicate key error you're hitting with MongoDB Converter in your IIS setup—totally get how frustrating this can be when things work once but break on repeat actions.
The Core Issue
That "item with the same key has already been added" error almost always happens because you're re-registering the same MongoDB class mappings every time you instantiate a MongoConverter. IIS's request/session lifecycle doesn't reset the application-level configuration when you clear a user session, so those mappings stick around between button clicks.
Fixes to Try
Let's break down actionable solutions, tailored for someone new to IIS and MongoDB:
1. Use a Singleton MongoConverter Instance
Instead of creating a new MongoConverter every time you click the button, make it a single global instance that's initialized once. This way, you only register your class mappings once, not on every request.
Example code (C#):
public static class MongoDbSetup { // Static converter that lives for the app's lifetime private static readonly MongoConverter _converter; static MongoDbSetup() { _converter = new MongoConverter(); // Register all your class mappings ONCE here _converter.RegisterClassMap<YourBoardModel>(); _converter.RegisterClassMap<OtherRelatedModel>(); } public static MongoConverter GetConverter() { return _converter; } }
Then in your button click handler, just call MongoDbSetup.GetConverter() instead of newing up a converter each time.
2. Register Mappings on Application Start
Move your MongoDB mapping registration to IIS's application startup event, so it runs once when your app first loads, not on every button click.
In your Global.asax.cs:
protected void Application_Start(object sender, EventArgs e) { // Initialize MongoDB mappings once here var converter = new MongoConverter(); converter.RegisterClassMap<YourBoardModel>(); // Store the converter in the application state for global access Application["MongoConverter"] = converter; }
Then in your button logic:
var converter = (MongoConverter)Application["MongoConverter"]; // Use the converter for your data operations
3. Check for Existing Mappings Before Registering
If you absolutely need to create a converter per request (not recommended, but sometimes necessary), add a check to avoid re-registering mappings:
var converter = new MongoConverter(); // Check if the class map already exists before registering if (BsonClassMap.LookupClassMap(typeof(YourBoardModel)) == null) { converter.RegisterClassMap<YourBoardModel>(); }
4. Understand IIS Session vs. App Domain
Quick note: Clearing a user's session doesn't reset the application's core configuration (like MongoDB mappings). Those mappings live in the IIS App Domain, which only resets when your app restarts (e.g., web.config changes, app pool recycle). So session cleanup won't fix this issue.
Final Tip
Start with the singleton approach—it's the cleanest way to avoid duplicate registration issues in IIS, and it aligns with how MongoDB's mapping system is designed to work.
内容的提问来源于stack exchange,提问作者Dlent

