ASP.NET Core 上 CloudTableClient class 的最佳 DI 注册范围是哪个
Which is the best DI registration Scope for CloudTableClient class on ASP.NET Core
我正在使用 ASP.NET Core 2.2 和 Azure Table 存储创建 Web 应用程序。由于 Microsoft 在 Azure Storage SDK 中为我们提供了 CloudTableClient
class,因此我将使用具有依赖注入 (DI) 的 class。然而,在标准的DI方法中,有AddScoped
、AddTransient
、AddSingleton
三种决定注册范围的方法。我的问题是哪个注册范围最适合 CloudTableClient
class。我认为 AddSingleton
是最好的,因为不会发生连接池饥饿,我将像附加的示例代码一样使用它。但是如果使用 AddSingleton
在某些方面(即性能或可靠性)不好,我想得到一些建议。
//Startup.cs
public void ConfigureServices(IServiceCollection services)
{
//do something
services.AddSingleton(provider =>
{
var settings = Configuration["AzureStorageConnectionString"];
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(settings);
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
return tableClient;
});
//do something
}
//SampleController
public class SampleController : Controller
{
private CloudTable _table { get; };
public SampleController(CloudTableClient tableClient)
{
_table = tableClient;
}
public async Task<IActionResult> GetSample(string id)
{
//do something with _table
}
}
AddScoped 然后它将为每个请求创建新的客户端或 AddTransient 每次你询问它都会给你新的实例。如果你做静态的,那么只有一个实例会为所有线程提供服务,这可能是一个问题,因为实例不给你保证它们是线程安全的
根据 published performance tips,使用单例是实现客户端的正确方法。
Each DocumentClient and CosmosClient instance is thread-safe and performs efficient connection management and address caching when operating in direct mode. To allow efficient connection management and better performance by the SDK client, it is recommended to use a single instance per AppDomain for the lifetime of the application.
我正在使用 ASP.NET Core 2.2 和 Azure Table 存储创建 Web 应用程序。由于 Microsoft 在 Azure Storage SDK 中为我们提供了 CloudTableClient
class,因此我将使用具有依赖注入 (DI) 的 class。然而,在标准的DI方法中,有AddScoped
、AddTransient
、AddSingleton
三种决定注册范围的方法。我的问题是哪个注册范围最适合 CloudTableClient
class。我认为 AddSingleton
是最好的,因为不会发生连接池饥饿,我将像附加的示例代码一样使用它。但是如果使用 AddSingleton
在某些方面(即性能或可靠性)不好,我想得到一些建议。
//Startup.cs
public void ConfigureServices(IServiceCollection services)
{
//do something
services.AddSingleton(provider =>
{
var settings = Configuration["AzureStorageConnectionString"];
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(settings);
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
return tableClient;
});
//do something
}
//SampleController
public class SampleController : Controller
{
private CloudTable _table { get; };
public SampleController(CloudTableClient tableClient)
{
_table = tableClient;
}
public async Task<IActionResult> GetSample(string id)
{
//do something with _table
}
}
AddScoped 然后它将为每个请求创建新的客户端或 AddTransient 每次你询问它都会给你新的实例。如果你做静态的,那么只有一个实例会为所有线程提供服务,这可能是一个问题,因为实例不给你保证它们是线程安全的
根据 published performance tips,使用单例是实现客户端的正确方法。
Each DocumentClient and CosmosClient instance is thread-safe and performs efficient connection management and address caching when operating in direct mode. To allow efficient connection management and better performance by the SDK client, it is recommended to use a single instance per AppDomain for the lifetime of the application.