I'm creating web application with ASP.NET Core 2.2 and Azure Table Storage. Since Microsoft provides us with CloudTableClient
class in Azure Storage SDK, I will use the class with Dependency Injection(DI). However, in standard DI approach, there are three methods to decide registration scope such as AddScoped
, AddTransient
, and AddSingleton
. My question is which registration scope is the best for CloudTableClient
class. I thought AddSingleton
is the best because connection pool starvation does not happen, and I will use it like attached sample code. But if using AddSingleton
is bad in some perspective(i.e. perf, or reliability), I would like to get some advice.
//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
}
}