Skip to content

Commit 1552ee3

Browse files
svrooijvnbaaij
andauthored
[General] Ability to change service lifetime (#2991)
* [General] Ability to change service lifetime Fixed #2691 * Moved Lifetime to LibraryConfiguration * Added dependency and removed two style warnings --------- Co-authored-by: Vincent Baaij <[email protected]>
1 parent 0aa0601 commit 1552ee3

File tree

5 files changed

+46
-9
lines changed

5 files changed

+46
-9
lines changed

examples/Demo/Shared/Microsoft.FluentUI.AspNetCore.Components.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15519,6 +15519,13 @@
1551915519
By default, the tooltip closes if the cursor leaves the anchor, but not the tooltip itself.
1552015520
</summary>
1552115521
</member>
15522+
<member name="P:Microsoft.FluentUI.AspNetCore.Components.LibraryConfiguration.ServiceLifetime">
15523+
<summary>
15524+
Gets or sets the service lifetime for the library services, when using Fluent UI in WebAssembly, it can make sense to use <see cref="F:Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton"/>.
15525+
Default is <see cref="F:Microsoft.Extensions.DependencyInjection.ServiceLifetime.Scoped"/>.
15526+
<para>Only <see cref="F:Microsoft.Extensions.DependencyInjection.ServiceLifetime.Scoped"/> and <see cref="F:Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton"/> are supported.</para>
15527+
</summary>
15528+
</member>
1552215529
<member name="P:Microsoft.FluentUI.AspNetCore.Components.LibraryConfiguration.ValidateClassNames">
1552315530
<summary>
1552415531
Gets or sets the value indicating whether the library should validate CSS class names.

src/Core/Components/DataGrid/Columns/ColumnBase.razor.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public abstract partial class ColumnBase<TGridItem>
2626
[Parameter]
2727
public string? Title { get; set; }
2828

29-
3029
/// <summary>
3130
/// Gets or sets the index (1-based) of the column
3231
/// </summary>

src/Core/Enums/DataGridRowSize.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public enum DataGridRowSize
1616
/// </summary>
1717
Medium = 44,
1818

19-
2019
/// <summary>
2120
/// Smaller row height
2221
/// </summary>

src/Core/Extensions/ServiceCollectionExtensions.cs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,41 @@ public static class ServiceCollectionExtensions
1414
/// <param name="configuration">Library configuration</param>
1515
public static IServiceCollection AddFluentUIComponents(this IServiceCollection services, LibraryConfiguration? configuration = null)
1616
{
17-
services.AddScoped<GlobalState>();
18-
services.AddScoped<IToastService, ToastService>();
19-
services.AddScoped<IDialogService, DialogService>();
20-
services.AddScoped<IMessageService, MessageService>();
21-
services.AddScoped<IKeyCodeService, KeyCodeService>();
22-
services.AddScoped<IMenuService, MenuService>();
17+
var serviceLifetime = configuration?.ServiceLifetime ?? ServiceLifetime.Scoped;
18+
if (serviceLifetime == ServiceLifetime.Transient)
19+
{
20+
throw new NotSupportedException("Transient lifetime is not supported for Fluent UI services.");
21+
}
22+
if (serviceLifetime == ServiceLifetime.Singleton)
23+
{
24+
services.AddSingleton<GlobalState>();
25+
services.AddSingleton<IToastService, ToastService>();
26+
services.AddSingleton<IDialogService, DialogService>();
27+
services.AddSingleton<IMessageService, MessageService>();
28+
services.AddSingleton<IKeyCodeService, KeyCodeService>();
29+
services.AddSingleton<IMenuService, MenuService>();
30+
}
31+
else
32+
{
33+
services.AddScoped<GlobalState>();
34+
services.AddScoped<IToastService, ToastService>();
35+
services.AddScoped<IDialogService, DialogService>();
36+
services.AddScoped<IMessageService, MessageService>();
37+
services.AddScoped<IKeyCodeService, KeyCodeService>();
38+
services.AddScoped<IMenuService, MenuService>();
39+
}
2340

2441
var options = configuration ?? new();
2542
if (options.UseTooltipServiceProvider)
2643
{
27-
services.AddScoped<ITooltipService, TooltipService>();
44+
if (serviceLifetime == ServiceLifetime.Singleton)
45+
{
46+
services.AddSingleton<ITooltipService, TooltipService>();
47+
}
48+
else
49+
{
50+
services.AddScoped<ITooltipService, TooltipService>();
51+
}
2852
}
2953
services.AddSingleton(options);
3054

src/Core/Infrastructure/LibraryConfiguration.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.AspNetCore.Components;
2+
using Microsoft.Extensions.DependencyInjection;
23

34
namespace Microsoft.FluentUI.AspNetCore.Components;
45

@@ -34,6 +35,13 @@ public class LibraryConfiguration
3435
/// </summary>
3536
public bool HideTooltipOnCursorLeave { get; set; } = false;
3637

38+
/// <summary>
39+
/// Gets or sets the service lifetime for the library services, when using Fluent UI in WebAssembly, it can make sense to use <see cref="ServiceLifetime.Singleton"/>.
40+
/// Default is <see cref="ServiceLifetime.Scoped"/>.
41+
/// <para>Only <see cref="ServiceLifetime.Scoped"/> and <see cref="ServiceLifetime.Singleton"/> are supported.</para>
42+
/// </summary>
43+
public ServiceLifetime ServiceLifetime { get; set; } = ServiceLifetime.Scoped;
44+
3745
/// <summary>
3846
/// Gets or sets the value indicating whether the library should validate CSS class names.
3947
/// respecting the following regex: "^-?[_a-zA-Z]+[_a-zA-Z0-9-]*$".

0 commit comments

Comments
 (0)