< Summary - Kestrun — Combined Coverage

Information
Class: Kestrun.Hosting.KestrunHostSessionExtensions
Assembly: Kestrun
File(s): /home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Hosting/KestrunHostSessionExtensions.cs
Tag: Kestrun/Kestrun@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
76%
Covered lines: 55
Uncovered lines: 17
Coverable lines: 72
Total lines: 172
Line coverage: 76.3%
Branch coverage
50%
Covered branches: 14
Total branches: 28
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 10/13/2025 - 16:52:37 Line coverage: 76.3% (55/72) Branch coverage: 50% (14/28) Total lines: 172 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e 10/13/2025 - 16:52:37 Line coverage: 76.3% (55/72) Branch coverage: 50% (14/28) Total lines: 172 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddSession(...)37.5%10870.58%
IsDistributedCacheRegistered(...)100%11100%
AddDistributedMemoryCache(...)37.5%13857.89%
AddStackExchangeRedisCache(...)66.66%6688.23%
AddDistributedSqlServerCache(...)66.66%6688.23%

File(s)

/home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Hosting/KestrunHostSessionExtensions.cs

#LineLine coverage
 1using Serilog.Events;
 2using Microsoft.Extensions.Caching.Memory;
 3using Microsoft.Extensions.Caching.Distributed;
 4using Microsoft.Extensions.Caching.StackExchangeRedis;
 5using Microsoft.Extensions.Caching.SqlServer;
 6using Microsoft.Extensions.Options;
 7
 8namespace Kestrun.Hosting;
 9
 10/// <summary>
 11/// Provides extension methods for configuring session state and distributed cache in Kestrun.
 12/// </summary>
 13public static class KestrunHostSessionExtensions
 14{
 15    /// <summary>
 16    /// Adds session state services and middleware to the application.
 17    /// </summary>
 18    /// <param name="host">The <see cref="KestrunHost" /> to add services to.</param>
 19    /// <param name="cfg">The configuration options for session state.</param>
 20    /// <returns>The updated <see cref="KestrunHost" /> instance.</returns>
 21    public static KestrunHost AddSession(this KestrunHost host, SessionOptions? cfg)
 22    {
 23        // Validate parameters
 224        ArgumentNullException.ThrowIfNull(host);
 25
 226        if (host.Logger.IsEnabled(LogEventLevel.Debug))
 27        {
 028            host.Logger.Debug("Adding Session with configuration: {@Config}", cfg);
 29        }
 30        // Avoid adding multiple session middlewares
 231        if (host.IsServiceRegistered(typeof(IConfigureOptions<SessionOptions>)))
 32        {
 033            throw new InvalidOperationException("Session services are already registered. Only one session configuration
 34        }
 35
 36        // Add the session services
 237        _ = host.AddService(services =>
 238        {
 139            _ = (cfg is null) ?
 140                services.AddSession() :
 141                services.AddSession(opts =>
 142                 {
 043                     opts.Cookie = cfg.Cookie;
 044                     opts.IdleTimeout = cfg.IdleTimeout;
 045                     opts.IOTimeout = cfg.IOTimeout;
 146                 });
 347        });
 48
 349        return host.Use(app => app.UseSession());
 50    }
 51
 52    /// <summary>
 53    /// Checks if a distributed cache implementation is already registered with the host.
 54    /// </summary>
 55    /// <param name="host">The <see cref="KestrunHost" /> to check.</param>
 56    /// <returns>true if a distributed cache is registered; otherwise, false.</returns>
 57    public static bool IsDistributedCacheRegistered(this KestrunHost host)
 58    {
 559        ArgumentNullException.ThrowIfNull(host);
 560        return host.IsServiceRegistered(typeof(IDistributedCache));
 61    }
 62
 63    /// <summary>
 64    /// Adds a default implementation of <see cref="IDistributedCache"/> that stores items in memory
 65    /// to the <see cref="KestrunHost" />. Frameworks that require a distributed cache to work
 66    /// can safely add this dependency as part of their dependency list to ensure that there is at least
 67    /// one implementation available.
 68    /// </summary>
 69    /// <param name="host">The <see cref="KestrunHost" /> to add services to.</param>
 70    /// <param name="cfg">The configuration options for the memory distributed cache.</param>
 71    /// <returns>The <see cref="KestrunHost"/> so that additional calls can be chained.</returns>
 72    public static KestrunHost AddDistributedMemoryCache(this KestrunHost host, MemoryDistributedCacheOptions? cfg)
 73    {
 374        ArgumentNullException.ThrowIfNull(host);
 375        if (host.Logger.IsEnabled(LogEventLevel.Debug))
 76        {
 077            host.Logger.Debug("Adding Distributed Memory Cache with configuration: {@Config}", cfg);
 78        }
 79
 80        // Avoid adding multiple distributed cache implementations
 381        if (IsDistributedCacheRegistered(host))
 82        {
 083            throw new InvalidOperationException("A distributed cache implementation is already registered. Only one dist
 84        }
 85
 86        // Add the distributed memory cache service
 387        return host.AddService(services =>
 388        {
 389            _ = (cfg is null) ?
 390                services.AddDistributedMemoryCache() :
 391                services.AddDistributedMemoryCache(opts =>
 392                {
 093                    opts.Clock = cfg.Clock;
 094                    opts.CompactionPercentage = cfg.CompactionPercentage;
 095                    opts.ExpirationScanFrequency = cfg.ExpirationScanFrequency;
 096                    opts.SizeLimit = cfg.SizeLimit;
 097                    opts.TrackLinkedCacheEntries = cfg.TrackLinkedCacheEntries;
 098                    opts.TrackStatistics = cfg.TrackStatistics;
 399                });
 6100        });
 101    }
 102
 103    /// <summary>
 104    /// Adds a StackExchange Redis implementation of <see cref="IDistributedCache"/> to the <see cref="KestrunHost" />.
 105    /// </summary>
 106    /// <param name="host">The <see cref="KestrunHost" /> to add services to.</param>
 107    /// <param name="cfg">The configuration options for the Redis cache.</param>
 108    /// <returns>The updated <see cref="KestrunHost" /> instance.</returns>
 109    public static KestrunHost AddStackExchangeRedisCache(this KestrunHost host, RedisCacheOptions cfg)
 110    {
 1111        ArgumentNullException.ThrowIfNull(host);
 1112        ArgumentNullException.ThrowIfNull(cfg);
 1113        if (host.Logger.IsEnabled(LogEventLevel.Debug))
 114        {
 0115            host.Logger.Debug("Adding StackExchange Redis Cache with configuration: {@Config}", cfg);
 116        }
 117
 118        // Avoid adding multiple distributed cache implementations
 1119        if (host.IsDistributedCacheRegistered())
 120        {
 0121            throw new InvalidOperationException("A distributed cache implementation is already registered. Only one dist
 122        }
 123
 124        // Ensure that the ConnectionMultiplexerFactory is set to avoid issues with multiple registrations
 1125        return host.AddService(services =>
 1126        {
 1127            _ = services.AddStackExchangeRedisCache(opts =>
 1128                {
 1129                    opts.Configuration = cfg.Configuration;
 1130                    opts.ConfigurationOptions = cfg.ConfigurationOptions;
 1131                    opts.InstanceName = cfg.InstanceName;
 1132                    opts.ProfilingSession = cfg.ProfilingSession;
 1133                    opts.ConnectionMultiplexerFactory = cfg.ConnectionMultiplexerFactory;
 2134                });
 2135        });
 136    }
 137
 138    /// <summary>
 139    /// Adds a SQL Server implementation of <see cref="IDistributedCache"/> to the <see cref="KestrunHost" />.
 140    /// </summary>
 141    /// <param name="host">The <see cref="KestrunHost" /> to add services to.</param>
 142    /// <param name="cfg">The configuration options for the SQL Server cache.</param>
 143    /// <returns>The updated <see cref="KestrunHost" /> instance.</returns>
 144    public static KestrunHost AddDistributedSqlServerCache(this KestrunHost host, SqlServerCacheOptions cfg)
 145    {
 1146        ArgumentNullException.ThrowIfNull(host);
 1147        ArgumentNullException.ThrowIfNull(cfg);
 1148        if (host.Logger.IsEnabled(LogEventLevel.Debug))
 149        {
 0150            host.Logger.Debug("Adding Distributed SQL Server Cache with configuration: {@Config}", cfg);
 151        }
 152
 153        // Avoid adding multiple distributed cache implementations
 1154        if (host.IsDistributedCacheRegistered())
 155        {
 0156            throw new InvalidOperationException("A distributed cache implementation is already registered. Only one dist
 157        }
 158
 159        // Ensure that the ConnectionMultiplexerFactory is set to avoid issues with multiple registrations
 1160        return host.AddService(services =>
 1161        {
 1162            _ = services.AddDistributedSqlServerCache(opts =>
 1163                {
 1164                    opts.ConnectionString = cfg.ConnectionString;
 1165                    opts.SchemaName = cfg.SchemaName;
 1166                    opts.TableName = cfg.TableName;
 1167                    opts.ExpiredItemsDeletionInterval = cfg.ExpiredItemsDeletionInterval;
 1168                    opts.DefaultSlidingExpiration = cfg.DefaultSlidingExpiration;
 2169                });
 2170        });
 171    }
 172}