Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//-----------------------------------------------------------------------
// <copyright file="AzureTableStorageConfigurationExtensions.cs" company="Marimer LLC">
// Copyright (c) Marimer LLC. All rights reserved.
// Website: https://cslanet.com
// </copyright>
// <summary>Configuration extensions for the Azure Table Storage session store</summary>
//-----------------------------------------------------------------------

using Azure.Data.Tables;
using Csla.Blazor.State.AzureTableStorage;
using Csla.State;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace Csla.Configuration
{
/// <summary>
/// Implement extension methods to register the Azure Table Storage
/// session store with the CSLA configuration system.
/// </summary>
public static class AzureTableStorageConfigurationExtensions
{
/// <summary>
/// Configures CSLA to use the Azure Table Storage
/// <see cref="ISessionStore"/> implementation.
/// </summary>
/// <param name="config">The <see cref="CslaOptions"/> instance.</param>
/// <param name="options">An optional delegate to configure the store.</param>
/// <returns>The <see cref="CslaOptions"/> instance.</returns>
/// <remarks>
/// Call this after <c>AddServerSideBlazor</c>, which registers the default
/// in-memory session store; the last registration wins. The session store
/// is only used when <c>UseInMemoryApplicationContextManager</c> is
/// <see langword="false"/>. Call <see cref="ISessionManager.PurgeSessions"/>
/// periodically: besides removing expired sessions, it refreshes the stored
/// last-touched time of live sessions, so other servers do not purge them.
/// <para>
/// Live sessions are kept in memory. Session data is written to the table
/// when a session is saved through <see cref="ISessionManager.UpdateSession"/>,
/// such as when the WebAssembly client sends its state; changes made in
/// place are written with the next save. A session can serialize to at
/// most 704 KiB.
/// </para>
/// <para>
/// The <see cref="TableServiceClient"/> comes from
/// <see cref="AzureTableStorageOptions.ConnectionString"/> when it is set,
/// otherwise from the keyed service named by
/// <see cref="AzureTableStorageOptions.ServiceKey"/>, otherwise from the
/// default <see cref="TableServiceClient"/> registered in the container.
/// </para>
/// </remarks>
/// <exception cref="ArgumentNullException"><paramref name="config"/> is <see langword="null"/>.</exception>
public static CslaOptions UseAzureTableStorageSessionStore(this CslaOptions config, Action<AzureTableStorageOptions>? options = null)
{
ArgumentNullException.ThrowIfNull(config);

var storeOptions = new AzureTableStorageOptions();
options?.Invoke(storeOptions);
Comment thread
joshhanson314 marked this conversation as resolved.
Resolved

// Singletons, because the singleton SessionManager depends on ISessionStore.
config.Services.AddSingleton(storeOptions);
config.Services.TryAddSingleton(TimeProvider.System);

// The client is resolved privately rather than registered, so the store
// never changes which TableServiceClient the rest of the app receives.
config.Services.AddSingleton<ISessionStore>(sp =>
ActivatorUtilities.CreateInstance<AzureTableStorageSessionStore>(
sp, GetTableServiceClient(sp, sp.GetRequiredService<AzureTableStorageOptions>())));
return config;
}

internal static TableServiceClient GetTableServiceClient(IServiceProvider serviceProvider, AzureTableStorageOptions options)
{
if (!string.IsNullOrEmpty(options.ConnectionString))
return new TableServiceClient(options.ConnectionString);
if (options.ServiceKey is not null)
return serviceProvider.GetRequiredKeyedService<TableServiceClient>(options.ServiceKey);
return serviceProvider.GetRequiredService<TableServiceClient>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//-----------------------------------------------------------------------
// <copyright file="AzureTableStorageOptions.cs" company="Marimer LLC">
// Copyright (c) Marimer LLC. All rights reserved.
// Website: https://cslanet.com
// </copyright>
// <summary>Options for the Azure Table Storage session store</summary>
//-----------------------------------------------------------------------

namespace Csla.Blazor.State.AzureTableStorage;

/// <summary>
/// Options for configuring the Azure Table Storage
/// <see cref="Csla.State.ISessionStore"/> implementation.
/// </summary>
public class AzureTableStorageOptions
{
/// <summary>
/// Gets or sets the Azure Storage connection string used to
/// create the <see cref="Azure.Data.Tables.TableServiceClient"/>.
/// </summary>
/// <remarks>
/// Leave <see langword="null"/> or empty to use a
/// <see cref="Azure.Data.Tables.TableServiceClient"/> that is already
/// registered in the dependency injection container (for example, one
/// registered by .NET Aspire or by managed-identity wiring).
/// </remarks>
public string? ConnectionString { get; set; }

/// <summary>
/// Gets or sets the dependency injection service key used to resolve a
/// keyed <see cref="Azure.Data.Tables.TableServiceClient"/>.
/// </summary>
/// <remarks>
/// Set this when the host registered the client as a keyed service (for
/// example <c>builder.AddKeyedAzureTableClient("state")</c>). Ignored when
/// <see cref="ConnectionString"/> is supplied.
/// </remarks>
public object? ServiceKey { get; set; }

/// <summary>
/// Gets or sets the name of the Azure Storage table used to store
/// sessions. Defaults to <c>CslaStateSessionManager</c>.
/// </summary>
public string TableName { get; set; } = "CslaStateSessionManager";

/// <summary>
/// Gets or sets a value used to discriminate sessions belonging to this
/// application when multiple applications share the same storage table.
/// Defaults to an empty string.
/// </summary>
public string ApplicationDiscriminator { get; set; } = string.Empty;
}
Loading
Loading