Skip to content

Commit 2ddbc21

Browse files
authored
CSHARP-4843: Suppress any exceptions in the code checking if legacy driver was loaded (#1214)
1 parent 96fa5f2 commit 2ddbc21

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/MongoDB.Driver.Core/Core/Connections/ClientDocumentHelper.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ internal static BsonDocument CreateDriverDocument()
7272
internal static BsonDocument CreateDriverDocument(string driverVersion)
7373
{
7474
var driverName = "mongo-csharp-driver";
75-
if (IsLegacyLoaded())
75+
if (TryGetType("MongoDB.Driver.MongoServer, MongoDB.Driver.Legacy"))
7676
{
7777
driverName = $"{driverName}|legacy";
7878
}
7979

80-
if (IsEFLoaded())
80+
if (TryGetType("MongoDB.EntityFrameworkCore.Query.MongoQueryContext, MongoDB.EntityFrameworkCore"))
8181
{
8282
driverName = $"{driverName}|efcore";
8383
}
@@ -87,16 +87,6 @@ internal static BsonDocument CreateDriverDocument(string driverVersion)
8787
{ "name", driverName },
8888
{ "version", driverVersion }
8989
};
90-
91-
bool IsLegacyLoaded()
92-
{
93-
return Type.GetType("MongoDB.Driver.MongoServer, MongoDB.Driver.Legacy") != null;
94-
}
95-
96-
bool IsEFLoaded()
97-
{
98-
return Type.GetType("MongoDB.EntityFrameworkCore.Query.MongoQueryContext, MongoDB.EntityFrameworkCore") != null;
99-
}
10090
}
10191

10292
internal static BsonDocument CreateEnvDocument()
@@ -193,7 +183,7 @@ internal static BsonDocument CreateOSDocument()
193183
string architecture;
194184
string osVersion;
195185

196-
if (Type.GetType("Mono.Runtime") != null)
186+
if (TryGetType("Mono.Runtime"))
197187
{
198188
switch (Environment.OSVersion.Platform)
199189
{
@@ -381,6 +371,20 @@ internal static BsonDocument RemoveOptionalFieldsUntilDocumentIsLessThan512Bytes
381371

382372
return clientDocument;
383373
}
374+
375+
private static bool TryGetType(string typeName)
376+
{
377+
try
378+
{
379+
var type = Type.GetType(typeName);
380+
return type != null;
381+
}
382+
catch
383+
{
384+
// ignore any exceptions here.
385+
return false;
386+
}
387+
}
384388
#endregion
385389
}
386390
}

0 commit comments

Comments
 (0)