title: MongoDB driver unable cast custom serializer to IBsonSerializer during filter by Id
body: <p>I am working on an application in C#. I have some problem getting a document by Id from MongoDB.</p>
  
  <p>My Id is not ObjectId but Guid.</p>
  
  <p>Model is simple POCO.     </p>
  
  <pre><code>public class Folder
  {
          public Guid Id { get; set; }
          public int OrgId { get; set; }
          // rest props is not important
  }
  </code></pre>
  
  <p>During creation of the repository instance (static constructor), I register class map:</p>
  
  <pre><code>    static MongoDBFolderRepository()
      {
          Init();
      }
  
      private static void Init()
      {
          var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() };
          ConventionRegistry.Register("CamelCase", camelCaseConvention, type =&gt; true);
  
          BsonClassMap.RegisterClassMap&lt;Folder&gt;(cm =&gt;
          {
              cm.AutoMap();
              cm.MapIdMember(c =&gt; c.Id).SetSerializer(new MyGuidSerializer());
              cm.SetIgnoreExtraElements(true);
          });
      }
  </code></pre>
  
  <p>And I have also specified my own serializer:</p>
  
  <pre><code>[BsonSerializer(typeof(MyGuidSerializer))]
  public class MyGuidSerializer : IBsonSerializer
  {
      public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
      {
          return Guid.Parse(BsonSerializer.Deserialize&lt;string&gt;(context.Reader));
      }
  
      public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
      {
          BsonSerializer.Serialize(context.Writer, value.ToString());
      }
  
      public Type ValueType
      {
          get { return typeof(Guid); }
      }
  }
  </code></pre>
  
  <p>Now this is the method which throwing an exception:</p>
  
  <pre><code>public async Task&lt;Folder&gt; GetFolderAsync(Guid folderId)
  {
      var database = MongoDBHelper.GetDatabase();
      var collection = database.GetCollection&lt;Folder&gt;(CollectionName);
      return await collection.Find(Builders&lt;Folder&gt;.Filter.Eq(x =&gt; x.Id, folderId)).SingleAsync();
  }
  </code></pre>
  
  <p>Exception is:</p>
  
  <blockquote>
    <p>Unable to cast object of type 'MyApp.Repositories.Reporting.Concrete.MyGuidSerializer' to type 'MongoDB.Bson.Serialization.IBsonSerializer`1[System.Guid]'.</p>
    
    <p>System.InvalidCastException</p>
  </blockquote>
  
  <p>Generally serialization works. If I change method to this version:</p>
  
  <pre><code>    public async Task&lt;Folder&gt; GetFolderAsync(Guid folderId)
      {
          var database = MongoDBHelper.GetDatabase();
          var collection = database.GetCollection&lt;Folder&gt;(CollectionName);
          return await collection.Find(Builders&lt;Folder&gt;.Filter.Eq("_id", folderId.ToString())).SingleAsync();
      }
  </code></pre>
  
  <p>I will got in return correctly filled <code>Folder</code> model.</p>
  
  <p>What did I do wrong ?</p>