You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#使用MongoDB遇System.MissingMethodException错误求助

MongoDB连接时的MissingMethodException异常解决方法

在C#项目中使用MongoDB官方示例代码测试连接与查询时,持续抛出MissingMethodException异常,已尝试重装依赖、重启Visual Studio、更换低版本驱动、新建极简测试项目等操作,问题仍未解决。测试代码如下,确认代码逻辑无问题:

MongoClient dbClient = new MongoClient(uri);
var client = new MongoClient(uri);
var db = client.GetDatabase("PlayerData");
var coll = db.GetCollection<PlayerData>("data");
var cursor = coll.AsQueryable();

foreach (var document in cursor)
{
    Console.WriteLine(document);
}

异常信息

error! MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---&gt; System.MissingMethodException: void System.Security.Cryptography.Rfc2898DeriveBytes..ctor(string,byte[],int,System.Security.Cryptography.HashAlgorithmName)
                             at MongoDB.Driver.Core.Authentication.ScramShaAuthenticator+ClientFirst.Transition (MongoDB.Driver.Core.Authentication.SaslAuthenticator+SaslConversation conversation, System.Byte[] bytesReceivedFromServer) [0x000da] in <aa6b841a807344eea7c91441c9eace83>:0
                             at MongoDB.Driver.Core.Authentication.SaslAuthenticator.Transition (MongoDB.Driver.Core.Authentication.SaslAuthenticator+SaslConversation conversation, MongoDB.Driver.Core.Authentication.SaslAuthenticator+ISaslStep currentStep, MongoDB.Bson.BsonDocument result, MongoDB.Bson.BsonDocument& command) [0x00038] in <aa6b841a807344eea7c91441c9eace83>:0
                             at MongoDB.Driver.Core.Authentication.SaslAuthenticator.Authenticate (MongoDB.Driver.Core.Connections.IConnection connection, MongoDB.Driver.Core.Connections.ConnectionDescription description, System.Threading.CancellationToken cancellationToken) [0x0008c] in <aa6b841a807344eea7c91441c9eace83>:0
                             at MongoDB.Driver.Core.Authentication.DefaultAuthenticator.Authenticate (MongoDB.Driver.Core.Connections.IConnection connection, MongoDB.Driver.Core.Connections.ConnectionDescription description, System.Threading.CancellationToken cancellationToken) [0x0009c] in <aa6b841a807344eea7c91441c9eace83>:0
                             at MongoDB.Driver.Core.Authentication.AuthenticationHelper.Authenticate (MongoDB.Driver.Core.Connections.IConnection connection, MongoDB.Driver.Core.Connections.ConnectionDescription description, System.Collections.Generic.IReadOnlyList`1[T] authenticators, System.Threading.CancellationToken cancellationToken) [0x00040] in <aa6b841a807344eea7c91441c9eace83>:0
                             at MongoDB.Driver.Core.Connections.ConnectionInitializer.Authenticate (MongoDB.Driver.Core.Connections.IConnection connection, MongoDB.Driver.Core.Connections.ConnectionDescription description, System.Threading.CancellationToken cancellationToken) [0x00025] in <aa6b841a807344eea7c91441c9eace83>:0
                             at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelper (System.Threading.CancellationToken cancellationToken) [0x0003b] in <aa6b841a807344eea7c91441c9eace83>:0
                              --- End of inner exception stack trace ---
                             at MongoDB.Driver.Linq.Linq2Implementation.MongoQueryProviderImpl`1[TDocument].Execute (System.Linq.Expressions.Expression expression) [0x00031] in <de98c2ec1446409986bc8acf49d23a01>:0
                             at MongoDB.Driver.Linq.Linq2Implementation.MongoQueryableImpl`2[TInput,TOutput].GetEnumerator () [0x00000] in <de98c2ec1446409986bc8acf49d23a01>:0
                             at StaffUtils.Features.Database.DatabaseConnectionTest () [0x0003c] in <01495ab2108f41f29b96324db58e8300>:0

解决方法

  • 检查项目目标框架版本:异常根源是MongoDB驱动调用的Rfc2898DeriveBytes构造函数仅在**.NET Framework 4.7.2+或.NET Core 2.0+/NET 5+**中存在。如果项目使用更低版本框架,需升级至符合要求的版本。
  • 匹配驱动与框架版本:若无法升级框架,选择兼容旧框架的MongoDB驱动版本,例如MongoDB.Driver 2.12.x及以下版本支持.NET Framework 4.5+,更高版本需对应更高框架。
  • 清理项目缓存:删除项目目录下的bin和obj文件夹,重新构建项目,避免残留的旧依赖文件导致版本冲突。
  • 调整身份验证机制:如果MongoDB服务器使用SCRAM-SHA认证,而旧框架不支持该构造函数,可在连接字符串中临时指定authMechanism=PLAIN(仅限本地测试等安全环境),或优先升级框架以支持完整的SCRAM-SHA认证。

内容的提问来源于stack exchange,提问作者Denty

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 21:06:17