简体   繁体   English

在 ASP.NET-Core 服务中重用 MongoDB MongoClient

[英]Re-use MongoDB MongoClient in ASP.NET-Core service

So I'm building an ASP.NET-Core API connecting to a mongoDB instance.所以我正在构建一个 ASP.NET-Core API 连接到 mongoDB 实例。 I was reading through the official Microsoft tutorial regarding this topic and came across the linked code sample.我正在阅读有关此主题的官方 Microsoft 教程,并遇到了链接的代码示例。

Basically they instantiate a BookService and create a new instance of MongoClient in the scope of the constructor.基本上他们实例化一个BookService并在构造函数的MongoClient中创建一个新的 MongoClient 实例。

private readonly IMongoCollection<Book> _books;

public BookService(IBookstoreDatabaseSettings settings)
{
    var client = new MongoClient(settings.ConnectionString);
    var database = client.GetDatabase(settings.DatabaseName);
    _books = database.GetCollection<Book>(settings.BooksCollectionName);
}

As I understand this the _books collection would still work without the MongoClient instance present since it knows which collection it's assigned to and how to communicate with it BUT the mongoDB MongoClient re-use guidelines suggests to store a global/static instance of the client to re-use.据我了解, _books集合在没有MongoClient实例存在的情况下仍然可以工作,因为它知道它被分配到哪个集合以及如何与它通信但是mongoDB MongoClient重用指南建议存储客户端的全局/静态实例以重新-利用。 (I guess for the same port-exhaustion, etc. reason you would want to re-use HTTPClients? Also it supports internal connection pooling, which is nice!) (我猜是由于相同的端口耗尽等原因,您想重新使用 HTTPClients?它还支持内部连接池,这很好!)

Thinking further on what they imply I was quite sure it would be a bad idea to instantiate and immediately drop an instance for a client for each of my services.进一步思考它们的含义,我很确定为我的每个服务实例化并立即为客户端删除一个实例是一个坏主意。 But I dont't know anything about MongoDB on this scope.但是我对这个 scope 上的 MongoDB 一无所知。

I know it's just a tutorial and they tend to be the "quick and dirty" way of coding but since I'm new to this whole topic I just wanted to make sure I would start out properly.我知道这只是一个教程,它们往往是“快速而肮脏”的编码方式,但由于我对整个主题都是新手,我只是想确保我能正确开始。

So is it OK doing it the "Microsoft way" or should I just create a global instance for the client or a factory altogether?那么以“微软方式”来做这件事可以吗,还是我应该为客户端或工厂创建一个全局实例?

//Edit: //编辑:

For clarification: Would it be better to register the MongoClient as a Singleton on Startup and inject it into classes that need it OR use the method described above?澄清一下:最好在启动时将 MongoClient 注册为MongoClient并将其注入需要它的类或使用上述方法?

This is how I typically add Mongo to my pipelines:这就是我通常将 Mongo 添加到我的管道的方式:

services.AddSingleton<IMongoClient>(sp =>
{ 
    var connectionString = "";
    return new MongoClient(connectionString);
});
services.AddScoped(sp =>
{
    var client = sp.GetRequiredService<IMongoClient>();
    var database = "";
    return client.GetDatabase(database);
});

This gives me a scoped IDatabase instance I can inject anywhere I need it (while using just one singleton IMongoClient instance).这给了我一个范围内的IDatabase实例,我可以在任何我需要的地方注入它(同时只使用一个 singleton IMongoClient实例)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM