@@ -12,19 +12,43 @@ public static class ServiceCollectionExtensions
1212 /// </summary>
1313 /// <param name="services">Service collection</param>
1414 /// <param name="configuration">Library configuration</param>
15- public static IServiceCollection AddFluentUIComponents ( this IServiceCollection services , LibraryConfiguration ? configuration = null )
15+ /// <param name="serviceLifetime">In case you want to use any of the services outside of the request, you can change the lifetime to <see cref="ServiceLifetime.Singleton"/>. Normally all services are registered as <see cref="ServiceLifetime.Scoped"/>.</param>
16+ public static IServiceCollection AddFluentUIComponents ( this IServiceCollection services , LibraryConfiguration ? configuration = null , ServiceLifetime serviceLifetime = ServiceLifetime . Scoped )
1617 {
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 > ( ) ;
18+ if ( serviceLifetime == ServiceLifetime . Transient )
19+ {
20+ throw new ArgumentException ( "Transient lifetime is not supported for Fluent UI services." , nameof ( serviceLifetime ) ) ;
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
@@ -38,11 +62,12 @@ public static IServiceCollection AddFluentUIComponents(this IServiceCollection s
3862 /// </summary>
3963 /// <param name="services">Service collection</param>
4064 /// <param name="configuration">Library configuration</param>
41- public static IServiceCollection AddFluentUIComponents ( this IServiceCollection services , Action < LibraryConfiguration > configuration )
65+ /// <param name="serviceLifetime">In case you want to use any of the services outside of the request, you can change the lifetime to <see cref="ServiceLifetime.Singleton"/>. Normally all services are registered as <see cref="ServiceLifetime.Scoped"/>.</param>
66+ public static IServiceCollection AddFluentUIComponents ( this IServiceCollection services , Action < LibraryConfiguration > configuration , ServiceLifetime serviceLifetime = ServiceLifetime . Scoped )
4267 {
4368 LibraryConfiguration options = new ( ) ;
4469 configuration . Invoke ( options ) ;
4570
46- return AddFluentUIComponents ( services , options ) ;
71+ return AddFluentUIComponents ( services , options , serviceLifetime ) ;
4772 }
4873}
0 commit comments