Getting Started with ASP.NET Core and Dependency Injection in the ScalarDB Cluster .NET Client SDK
The ScalarDB Cluster .NET Client SDK supports Dependency Injection in frameworks like ASP.NET Core.
After installing the same major and minor version of the SDK as ScalarDB Cluster into the .NET project, you can register the ScalarDB Cluster transaction managers as services in IServiceCollection
as follows, replacing <HOSTNAME_OR_IP_ADDRESS>
with the FQDN or the IP address, and <PORT>
with the port number (60053
by default) of your cluster:
using ScalarDB.Net.Client.Extensions;
//...
var builder = WebApplication.CreateBuilder(args);
//...
builder.Services.AddScalarDbCluster(options =>
{
options.Address = "http://<HOSTNAME_OR_IP_ADDRESS>:<PORT>";
options.HopLimit = 10;
options.EnableCrud = true; // true by default
options.EnableSql = true; // false by default
options.EnableAdmin = true; // false by default
});
After registering the transaction managers, they can be injected into the controller's constructor as follows:
[ApiController]
public class OrderController: ControllerBase
{
private readonly IDistributedTransactionManager _manager;
public OrderController(IDistributedTransactionManager manager)
{
_manager = manager;
}
}
Although these examples are for WebApi projects, the examples will work in a similar way in GrpcService projects.
AddScalarDbCluster
-specific options​
This section describes some specific options for AddScalarDbCluster
:
- EnableCrud: Enables the transaction managers for executing CRUD operations.
IDistributedTransactionManager
andITwoPhaseCommitTransactionManager
will become available for injection. - EnableSql: Enables the transaction managers for executing SQL statements.
ISqlTransactionManager
andISqlTwoPhaseCommitTransactionManager
will become available for injection. - EnableAdmin: Enables the administrative interface.
IDistributedTransactionAdmin
will become available for injection.