受欢迎的博客标签

1.MongoDB Driver Exception in Preview 4 

I installed .NET Core Preview 4, startup my app, and received this exception. This is a .NET Core Console Application that is referencing an object in a .NET Standard Library. The types all appear appear in the debugger however the mongo driver throws this exception.

I moved back to .NET Core Preview 3, recompiled, and there is no issue.

System.TypeInitializationException
  HResult=0x80131534
  Message=The type initializer for 'MongoDB.Bson.Serialization.BsonClassMap' threw an exception.
  Source=MongoDB.Bson
  StackTrace:
   at MongoDB.Bson.Serialization.BsonClassMap.LookupClassMap(Type classType)
   at MongoDB.Bson.Serialization.BsonClassMapSerializationProvider.GetSerializer(Type type, IBsonSerializerRegistry serializerRegistry)
   at MongoDB.Bson.Serialization.BsonSerializerRegistry.CreateSerializer(Type type)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at MongoDB.Bson.Serialization.BsonSerializerRegistry.GetSerializer[T]()
   at MongoDB.Driver.MongoCollectionImpl`1..ctor(IMongoDatabase database, CollectionNamespace collectionNamespace, MongoCollectionSettings settings, ICluster cluster, IOperationExecutor operationExecutor)
   at MongoDB.Driver.MongoDatabaseImpl.GetCollection[TDocument](String name, MongoCollectionSettings settings)
   ...

Inner Exception 1:
ArgumentNullException: Value cannot be null.
Parameter name: type

https://github.com/dotnet/core/issues/2611

2.MongoDB driver needs a reference to System.Runtime.Serialization.Formatters.

3.Fix initialization on .NET Core 3.0 preview 4

3.1 src/MongoDB.Bson/MongoDB.Bson.csproj 

   <PackageReference Include="System.Diagnostics.Process" Version="4.1.0" />
    <PackageReference Include="System.Dynamic.Runtime" Version="4.0.11" />
    <PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.3.0" />
  + <PackageReference Include="System.Runtime.Serialization.Formatters" Version="4.3.0" />
  </ItemGroup>

3.2 src/MongoDB.Bson/Serialization/BsonClassMap.cs

using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
-#if NET452
using System.Runtime.Serialization;
-#endif
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization.Conventions;

 

src/MongoDB.Bson/Serialization/BsonClassMap.cs 
@@ -38,10 +38,7 @@ public class BsonClassMap
        private readonly static Queue<Type> __knownTypesQueue = new Queue<Type>();

        private static readonly MethodInfo __getUninitializedObjectMethodInfo =
          -  typeof(string)
          -  .GetTypeInfo()
          -   .Assembly
          -  .GetType("System.Runtime.Serialization.FormatterServices")
          +  GetFormatterServicesType()
            .GetTypeInfo()
            ?.GetMethod("GetUninitializedObject", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);

@@ -1306,7 +1303,21 @@ private void ThrowNotFrozenException()
            var message = string.Format("Class map for {0} has been not been frozen yet.", _classType.FullName);
            throw new InvalidOperationException(message);
        }
    }

        private static Type GetFormatterServicesType()
        {
#if NET452
            return typeof(string)
                .GetTypeInfo()
                .Assembly
                .GetType("System.Runtime.Serialization.FormatterServices");
#else
            return 
                Assembly.Load(new AssemblyName("System.Runtime.Serialization.Formatters"))
                .GetType("System.Runtime.Serialization.FormatterServices");
#endif
        }
	}

https://github.com/rstam/mongo-csharp-driver/pull/110/commits/82c44c3896967863b43f12729fcccd781d9dca6d

https://github.com/rstam/mongo-csharp-driver/pull/110/commits/888100db153ad510a97c8e6f6875d4c7c88693b9