[CSHARP-4843] Suppress any exceptions in the code checking for legacy driver loaded Created: 13/Nov/23  Updated: 23/Nov/23  Resolved: 23/Nov/23

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 2.22.0
Fix Version/s: 2.23.0

Type: Bug Priority: Blocker - P1
Reporter: Wesam Abdallah Assignee: Oleksandr Poliakov
Resolution: Fixed Votes: 0
Labels: MongoDB.Driver.Core.Connections.ClientDocumentHelper, NRE
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: PNG File image-2023-11-13-17-40-06-682.png     PNG File image-2023-11-14-21-24-38-091.png    
Backwards Compatibility: Fully Compatible
Documentation Changes: Not Needed
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Summary

When using MongoDB Driver from NuGet, v 2.22.0

When instantiating an **

new MongoClient(connectionString)

 instance, with a connection string pointing to

mongodb://localhost:27017

The MongoClient object is called from a .Net 2.0 Standard library with 4.8 as Target, it runs as a Windows Service.
 
I get the following error.
 

"exception": "System.NullReferenceException: Object reference not set to an instance of an object.
at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName, ObjectHandleOnStack type)
at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName)
at System.Type.GetType(String typeName)
at MongoDB.Driver.Core.Connections.ClientDocumentHelper.<CreateDriverDocument>g__IsLegacyLoaded|9_0()
at MongoDB.Driver.Core.Connections.ClientDocumentHelper.CreateDriverDocument(String driverVersion)
at System.Lazy`1.CreateValue()
— End of stack trace from previous location where exception was thrown —
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Lazy`1.get_Value()
at MongoDB.Driver.Core.Connections.ClientDocumentHelper.CreateClientDocument(String applicationName)
at MongoDB.Driver.Core.Connections.ConnectionInitializer..ctor(String applicationName, IReadOnlyList`1 compressors, ServerApi serverApi)
at MongoDB.Driver.Core.Connections.BinaryConnectionFactory..ctor(ConnectionSettings settings, IStreamFactory streamFactory, IEventSubscriber eventSubscriber, ServerApi serverApi, ILoggerFactory loggerFactory)
at MongoDB.Driver.Core.Configuration.ClusterBuilder.CreateConnectionPoolFactory()
at MongoDB.Driver.Core.Configuration.ClusterBuilder.CreateServerFactory()
at MongoDB.Driver.Core.Configuration.ClusterBuilder.CreateClusterFactory()
at MongoDB.Driver.ClusterRegistry.CreateCluster(ClusterKey clusterKey)
at MongoDB.Driver.ClusterRegistry.GetOrCreateCluster(ClusterKey clusterKey)
at MongoDB.Driver.MongoClient..ctor(MongoClientSettings settings)

The same library has no issues connecting to the same Mongo instance when running inside of an Asp.Net 4.8 service.



 Comments   
Comment by Githook User [ 23/Nov/23 ]

Author:

{'name': 'Oleksandr Poliakov', 'email': '31327136+sanych-sun@users.noreply.github.com', 'username': 'sanych-sun'}

Message: CSHARP-4843: Suppress any exceptions in the code checking if legacy driver was loaded (#1214)
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/2ddbc21240e4e7ddd0c3b4912834cccb5adf2297

Comment by Oleksandr Poliakov [ 15/Nov/23 ]

Sure, that's definitely makes sense to suppress any exception in that code block. I'm just trying to reproduce the issue, so I can ensure it's gone after the changes.

Comment by Wesam Abdallah [ 15/Nov/23 ]

One aspect worth noting is that the calling function code is running contextually inside of Windows service.

If you are trying to reproduce this issue, create a Windows service with the new driver

 

What would be the harm in wrapping up those checks in try/catch?

https://github.com/mongodb/mongo-csharp-driver/blob/master/src/MongoDB.Driver.Core/Core/Connections/ClientDocumentHelper.cs

 

bool IsLegacyLoaded()
            {
                return Type.GetType("MongoDB.Driver.MongoServer, MongoDB.Driver.Legacy") != null;
            }            

bool IsEFLoaded()
            {
                return Type.GetType("MongoDB.EntityFrameworkCore.Query.MongoQueryContext, MongoDB.EntityFrameworkCore") != null;
            } 

 

 

Comment by Wesam Abdallah [ 15/Nov/23 ]

 

The exception happens because "I think" this function is called from a cpp handler

 

Others are also dealing with this, they have to add the `mongocsharpdriver` in addition to the new MongoDB.Driver
https://www.mongodb.com/community/forums/t/mongodb-driver-2-18-net-4-72/198863/2

https://stackoverflow.com/questions/29441456/mongodb-drivers-query-builder-missing-from-drivers

 

Comment by Oleksandr Poliakov [ 14/Nov/23 ]

Hi wamra2004-developer@yahoo.com !

Thank you for reporting the problem. We do not have hard dependency onto the legacy driver, we rather checking if the legacy driver is present in the app domain and update MongoClient properties accordingly (you can see this as a User-Agent string).

Talking about the exception itself: it looks a little weird, because the Type.GetType method should not throw if type is not available accordingly to the documentation

And as a result I cannot reproduce the issue. Could you please try to create a minimal application to reproduce the issue? Also some additional information could help: like a is there any frameworks being used that might affect type/assembly loading, some hosting details (net framework version? is it mono?)

 

Thanks,

Oleksandr Poliakov

Comment by Wesam Abdallah [ 13/Nov/23 ]

This a very bad mistake..

I looked further into it, we are not using MongoServer, we are only using the new MongoClient... Why is the driver throwing an exception while checking if the legacy type/lib is available?

You introduced a dependency from the new API to the older legacy `MongoDB.Driver.MongoServer, MongoDB.Driver.Legacy`

https://github.com/mongodb/mongo-csharp-driver/blob/8e4340410614c38096befa4dee27d276f85db207/src/MongoDB.Driver.Core/Core/Connections/ClientDocumentHelper.cs#L93

The issues in this section ...

```
    bool IsLegacyLoaded()
           

{                 return Type.GetType("MongoDB.Driver.MongoServer, MongoDB.Driver.Legacy") != null;             }

```

This type was removed from the latest MongoDB.Driver.Legacy 2.22.0

Please remove this dependency and release an update

Comment by PM Bot [ 13/Nov/23 ]

Hi wamra2004-developer@yahoo.com, thank you for reporting this issue! The team will look into it and get back to you soon.

Generated at Wed Feb 07 21:49:32 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.