2

In my ASP.NET core Web API, I need to use MongoDb. Following is my implementation so far but I am stuck in resolving dependencies.

DataContext:

 public class AppDbContext
    {
        public IMongoDatabase MongoDatabase { get; set; }
        public AppDbContext()
        {
            var client = new MongoClient("mongodb://localhost:27017");
            MongoDatabase = client.GetDatabase("cse-dev-db");
        }
    }

Repository:

public class BuyRepository: IBuyRepository {
    private readonly AppDbContext _appDbContext;
    public BuyRepository(AppDbContext appDbContext) {
        _appDbContext = appDbContext;
    }
    public Buy Add(Buy buy) {
        _appDbContext.MongoDatabase.GetCollection<Buy("Buy").InsertOne(buy);
        return buy;
    }
}

Controller:

private readonly BuyRepository _buyRepository;
public ValuesController(BuyRepository buyRepository) {
    _buyRepository = buyRepository;
}

My question is how to add this dependencies in ConfigureServices

public void ConfigureServices(IServiceCollection services) {
    services.AddApplicationInsightsTelemetry(Configuration);
    services.AddMvc();
    // How to add dependencies here
}

PS: I have already seen this but it does not work.

Update

I have tried as per comment by a user

public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddApplicationInsightsTelemetry(Configuration);
            services.AddScoped<AppDbContext>();
            services.AddMvc();
            services.AddScoped<IBuyRepository, BuyRepository>();
        }

Now I am getting following exception

Unable to resolve service for type 'CseApi.Repositories.BuyRepository' while attempting to activate 'CseApi.Controllers.ValuesController'.

5
  • I'm not sure if it is not working I need to check it manually. It strange that mongo db post article which is not tested. Commented Oct 5, 2016 at 5:32
  • Are you using a NuGet package to add support for MongoDB? Commented Oct 5, 2016 at 5:49
  • Yes "mongocsharpdriver": "2.3.0" Commented Oct 5, 2016 at 5:50
  • It's throwing exception. Please see my updated question Commented Oct 5, 2016 at 6:02
  • You need to inject the interface IBuyStockRepository in the ValuesController constructor, not the concrete type. Commented Oct 5, 2016 at 6:04

2 Answers 2

3

Try to register services like:

public void ConfigureServices(IServiceCollection services) {
    services.AddApplicationInsightsTelemetry(Configuration);
    services.AddScoped<AppDbContext>();
    services.AddScoped<IBuyRepository, BuyRepository>();
    services.AddMvc();
    // How to add dependencies here
}

Update for comment

Controller code should be something like below:

private readonly IBuyRepository _buyRepository;
public ValuesController(IBuyRepository buyRepository) {
    _buyRepository = buyRepository;
}
Sign up to request clarification or add additional context in comments.

3 Comments

It's throwing exception Unable to resolve service for type 'CseApi.Repositories.BuyRepository' while attempting to activate 'CseApi.Controllers.ValuesController'
Your controller should take IBuyRepository not BuyRepository. See my update.
Yes That was the problem. Thank you
0

Update your controller's injection from this:

private readonly BuyRepository _buyRepository;

to this:

private readonly IBuyRepository _buyRepository;

Comments

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.