Files
git.stella-ops.org/src/Concelier/__Libraries/StellaOps.Concelier.Storage.Mongo/MongoSessionProvider.cs
2025-10-28 15:10:40 +02:00

35 lines
1.2 KiB
C#

using Microsoft.Extensions.Options;
using MongoDB.Driver;
namespace StellaOps.Concelier.Storage.Mongo;
public interface IMongoSessionProvider
{
Task<IClientSessionHandle> StartSessionAsync(CancellationToken cancellationToken = default);
}
internal sealed class MongoSessionProvider : IMongoSessionProvider
{
private readonly IMongoClient _client;
private readonly MongoStorageOptions _options;
public MongoSessionProvider(IMongoClient client, IOptions<MongoStorageOptions> options)
{
_client = client ?? throw new ArgumentNullException(nameof(client));
_options = options?.Value ?? throw new ArgumentNullException(nameof(options));
}
public Task<IClientSessionHandle> StartSessionAsync(CancellationToken cancellationToken = default)
{
var sessionOptions = new ClientSessionOptions
{
DefaultTransactionOptions = new TransactionOptions(
readPreference: ReadPreference.Primary,
readConcern: ReadConcern.Majority,
writeConcern: WriteConcern.WMajority.With(wTimeout: _options.CommandTimeout))
};
return _client.StartSessionAsync(sessionOptions, cancellationToken);
}
}