2

So I have the following MongoDBService that I have created:

public class MongoDBService
{
    private const String CONNECTION_STRING = "mongodb://localhost:27017";
    private const String DATABASE_NAME = "TestingTesting";
    private readonly MongoClient _Client;

    public MongoDBService()
    {
        _Client = new MongoClient(CONNECTION_STRING);
    }

    public IMongoCollection<T> GetCollection<T>()
    {
        return _Client.GetDatabase(DATABASE_NAME).GetCollection<T>(typeof(T).Name);
    }
}

I have set MongoDBService as a service in my asp.net core Startup.cs:

services.AddSingleton<MongoDBService>();

I am then trying to use the service by injecting it into the .razor page:

@inject Project.Services.MongoDBService

I am also initiating an instance of my MongoDBService:

Project.Services.MongoDBService mongo = new Services.MongoDBService();

But when I then try to use it, I get a red squiggly line under the FindAsync() method:

async Task GetAllClocks()
{
    MongoDB.Driver.FilterDefinition<Project.Data.Clock> filter = MongoDB.Driver.Builders<Project.Data.Clock>.Filter.Empty;

    MongoDB.Driver.IMongoCollection<Project.Data.Clock> cursor = await mongo.GetCollection<Project.Data.Clock>().FindAsync(filter);
}

The error is:

CS0411: The type arguments for method 'IMongoCollection.FindAsync(FilterDefinition, FindOptions<Clock, TProjection>, CancellationToken)' cannot be inferred from the usage. Try specifying the type

I've been Googling for a while and while it sounds like I'm leaving the compiler to decide what type to use, I'm sure I am explicitly specifying the Project.Data.Clock type. Absolutely stumped. Any ideas?

2
  • 1
    A guess: FindOptions needs two type parameters, and you are only supplying one. If you wish to return a Clock object, try FindOptions<Clock, Clock>. Commented Oct 7, 2020 at 10:05
  • Hey Ricardo, you're actually right, I created new FindOptions instance and passed it as an argument to the FindAsync method. What is odd is that I've never had to do that before, this is the first time I am encountering this error. Commented Oct 7, 2020 at 10:27

1 Answer 1

2

FindOptions needs two type parameters, and you are only supplying one. If you wish to return a Clock object, try FindOptions<Clock, Clock>.

Sign up to request clarification or add additional context in comments.

3 Comments

Hey Ricardo, thanks for this. Any idea why in this instance I have had to pass a FindOptions parameter? Usually FilterDefintiion<>.Empty is enough to satisfy FindAsync()
In my machine, using NuGet packages 2.11.2, I don't need to pass the FindOptions parameter. What version are you using? What I have is: client.GetDatabase("<db>").GetCollection<Clock>("<collection>").FindAsync(filter)
My MongoDB.Driver and MongoDB.Driver.Core are both 2.11.2 too! Very odd.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.