@@ -21,6 +21,8 @@ namespace Microsoft.AspNetCore.Blazor.Hosting
21
21
/// </summary>
22
22
public sealed class WebAssemblyHostBuilder
23
23
{
24
+ private Func < IServiceProvider > _createServiceProvider ;
25
+
24
26
/// <summary>
25
27
/// Creates an instance of <see cref="WebAssemblyHostBuilder"/> using the most common
26
28
/// conventions and settings.
@@ -54,6 +56,11 @@ private WebAssemblyHostBuilder()
54
56
Services = new ServiceCollection ( ) ;
55
57
56
58
InitializeDefaultServices ( ) ;
59
+
60
+ _createServiceProvider = ( ) =>
61
+ {
62
+ return Services . BuildServiceProvider ( ) ;
63
+ } ;
57
64
}
58
65
59
66
/// <summary>
@@ -72,6 +79,45 @@ private WebAssemblyHostBuilder()
72
79
/// </summary>
73
80
public IServiceCollection Services { get ; }
74
81
82
+ /// <summary>
83
+ /// Registers a <see cref="IServiceProviderFactory{TBuilder}" /> instance to be used to create the <see cref="IServiceProvider" />.
84
+ /// </summary>
85
+ /// <param name="factory">The <see cref="IServiceProviderFactory{TBuilder}" />.</param>
86
+ /// <typeparam name="TBuilder">The type of builder provided by the <see cref="IServiceProviderFactory{TBuilder}" />.</typeparam>
87
+ public void ConfigureContainer < TBuilder > ( IServiceProviderFactory < TBuilder > factory )
88
+ {
89
+ if ( factory == null )
90
+ {
91
+ throw new ArgumentNullException ( nameof ( factory ) ) ;
92
+ }
93
+
94
+ ConfigureContainer ( factory , configure : null ) ;
95
+ }
96
+
97
+ /// <summary>
98
+ /// Registers a <see cref="IServiceProviderFactory{TBuilder}" /> instance to be used to create the <see cref="IServiceProvider" />.
99
+ /// </summary>
100
+ /// <param name="factory">The <see cref="IServiceProviderFactory{TBuilder}" />.</param>
101
+ /// <param name="configure">
102
+ /// A delegate used to configure the <typeparamref T="TBuilder" />. This can be used to configure services using
103
+ /// APIS specific to the <see cref="IServiceProviderFactory{TBuilder}" /> implementation.
104
+ /// </param>
105
+ /// <typeparam name="TBuilder">The type of builder provided by the <see cref="IServiceProviderFactory{TBuilder}" />.</typeparam>
106
+ public void ConfigureContainer < TBuilder > ( IServiceProviderFactory < TBuilder > factory , Action < TBuilder > configure )
107
+ {
108
+ if ( factory == null )
109
+ {
110
+ throw new ArgumentNullException ( nameof ( factory ) ) ;
111
+ }
112
+
113
+ _createServiceProvider = ( ) =>
114
+ {
115
+ var container = factory . CreateBuilder ( Services ) ;
116
+ configure ? . Invoke ( container ) ;
117
+ return factory . CreateServiceProvider ( container ) ;
118
+ } ;
119
+ }
120
+
75
121
/// <summary>
76
122
/// Builds a <see cref="WebAssemblyHost"/> instance based on the configuration of this builder.
77
123
/// </summary>
@@ -85,7 +131,7 @@ public WebAssemblyHost Build()
85
131
// A Blazor application always runs in a scope. Since we want to make it possible for the user
86
132
// to configure services inside *that scope* inside their startup code, we create *both* the
87
133
// service provider and the scope here.
88
- var services = Services . BuildServiceProvider ( ) ;
134
+ var services = _createServiceProvider ( ) ;
89
135
var scope = services . GetRequiredService < IServiceScopeFactory > ( ) . CreateScope ( ) ;
90
136
91
137
return new WebAssemblyHost ( services , scope , configuration , RootComponents . ToArray ( ) ) ;
0 commit comments