Skip to content

Commit f609ce8

Browse files
authored
EF-73: Add more EF Core metadata to Client Metadata Handshake (#1222)
1 parent 225ac8b commit f609ce8

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,23 @@ internal static BsonDocument CreateClientDocument(string applicationName, BsonDo
6363
internal static BsonDocument CreateDriverDocument()
6464
{
6565
var assembly = typeof(ConnectionInitializer).GetTypeInfo().Assembly;
66-
var fileVersionAttribute = assembly.GetCustomAttribute<AssemblyFileVersionAttribute>();
67-
var driverVersion = fileVersionAttribute.Version;
66+
var driverVersion = GetAssemblyVersion(assembly);
6867

6968
return CreateDriverDocument(driverVersion);
7069
}
7170

7271
internal static BsonDocument CreateDriverDocument(string driverVersion)
7372
{
7473
var driverName = "mongo-csharp-driver";
75-
if (TryGetType("MongoDB.Driver.MongoServer, MongoDB.Driver.Legacy"))
74+
if (TryGetType("MongoDB.Driver.MongoServer, MongoDB.Driver.Legacy", out _))
7675
{
7776
driverName = $"{driverName}|legacy";
7877
}
7978

80-
if (TryGetType("MongoDB.EntityFrameworkCore.Query.MongoQueryContext, MongoDB.EntityFrameworkCore"))
79+
if (TryGetType("MongoDB.EntityFrameworkCore.Query.MongoQueryContext, MongoDB.EntityFrameworkCore", out var queryContextType))
8180
{
81+
var efVersion = GetAssemblyVersion(queryContextType.Assembly);
82+
driverVersion = $"{driverVersion}|{efVersion}";
8283
driverName = $"{driverName}|efcore";
8384
}
8485

@@ -183,7 +184,7 @@ internal static BsonDocument CreateOSDocument()
183184
string architecture;
184185
string osVersion;
185186

186-
if (TryGetType("Mono.Runtime"))
187+
if (TryGetType("Mono.Runtime", out _))
187188
{
188189
switch (Environment.OSVersion.Platform)
189190
{
@@ -372,19 +373,33 @@ internal static BsonDocument RemoveOptionalFieldsUntilDocumentIsLessThan512Bytes
372373
return clientDocument;
373374
}
374375

375-
private static bool TryGetType(string typeName)
376+
private static bool TryGetType(string typeName, out Type type)
376377
{
377378
try
378379
{
379-
var type = Type.GetType(typeName);
380+
type = Type.GetType(typeName);
380381
return type != null;
381382
}
382383
catch
383384
{
384385
// ignore any exceptions here.
386+
type = null;
385387
return false;
386388
}
387389
}
390+
391+
private static string GetAssemblyVersion(Assembly assembly)
392+
{
393+
var versionAttribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
394+
var hashIndex = versionAttribute.InformationalVersion.IndexOf('+');
395+
if (hashIndex == -1)
396+
{
397+
return versionAttribute.InformationalVersion;
398+
}
399+
400+
return versionAttribute.InformationalVersion.Substring(0, hashIndex);
401+
}
402+
388403
#endregion
389404
}
390405
}

0 commit comments

Comments
 (0)