@@ -39,6 +39,37 @@ public static IWebHostBuilder UseIf(
3939 return hostBuilder ;
4040 }
4141
42+ /// <summary>
43+ /// Executes the specified action if the specified <paramref name="condition"/> is <c>true</c> which can be
44+ /// used to conditionally add to the host builder.
45+ /// </summary>
46+ /// <param name="hostBuilder">The host builder.</param>
47+ /// <param name="condition">If <c>true</c> is returned the action is executed.</param>
48+ /// <param name="action">The action used to add to the host builder.</param>
49+ /// <returns>The same host builder.</returns>
50+ public static IWebHostBuilder UseIf (
51+ this IWebHostBuilder hostBuilder ,
52+ Func < IWebHostBuilder , bool > condition ,
53+ Func < IWebHostBuilder , IWebHostBuilder > action )
54+ {
55+ if ( hostBuilder == null )
56+ {
57+ throw new ArgumentNullException ( nameof ( hostBuilder ) ) ;
58+ }
59+
60+ if ( action == null )
61+ {
62+ throw new ArgumentNullException ( nameof ( action ) ) ;
63+ }
64+
65+ if ( condition ( hostBuilder ) )
66+ {
67+ hostBuilder = action ( hostBuilder ) ;
68+ }
69+
70+ return hostBuilder ;
71+ }
72+
4273 /// <summary>
4374 /// Executes the specified <paramref name="ifAction"/> if the specified <paramref name="condition"/> is
4475 /// <c>true</c>, otherwise executes the <paramref name="elseAction"/>. This can be used to conditionally add to
@@ -82,5 +113,49 @@ public static IWebHostBuilder UseIfElse(
82113
83114 return hostBuilder ;
84115 }
116+
117+ /// <summary>
118+ /// Executes the specified <paramref name="ifAction"/> if the specified <paramref name="condition"/> is
119+ /// <c>true</c>, otherwise executes the <paramref name="elseAction"/>. This can be used to conditionally add to
120+ /// the host builder.
121+ /// </summary>
122+ /// <param name="hostBuilder">The host builder.</param>
123+ /// <param name="condition">If <c>true</c> is returned the <paramref name="ifAction"/> is executed, otherwise the
124+ /// <paramref name="elseAction"/> is executed.</param>
125+ /// <param name="ifAction">The action used to add to the host builder if the condition is <c>true</c>.</param>
126+ /// <param name="elseAction">The action used to add to the host builder if the condition is <c>false</c>.</param>
127+ /// <returns>The same host builder.</returns>
128+ public static IWebHostBuilder UseIfElse (
129+ this IWebHostBuilder hostBuilder ,
130+ Func < IWebHostBuilder , bool > condition ,
131+ Func < IWebHostBuilder , IWebHostBuilder > ifAction ,
132+ Func < IWebHostBuilder , IWebHostBuilder > elseAction )
133+ {
134+ if ( hostBuilder == null )
135+ {
136+ throw new ArgumentNullException ( nameof ( hostBuilder ) ) ;
137+ }
138+
139+ if ( ifAction == null )
140+ {
141+ throw new ArgumentNullException ( nameof ( ifAction ) ) ;
142+ }
143+
144+ if ( elseAction == null )
145+ {
146+ throw new ArgumentNullException ( nameof ( elseAction ) ) ;
147+ }
148+
149+ if ( condition ( hostBuilder ) )
150+ {
151+ hostBuilder = ifAction ( hostBuilder ) ;
152+ }
153+ else
154+ {
155+ hostBuilder = elseAction ( hostBuilder ) ;
156+ }
157+
158+ return hostBuilder ;
159+ }
85160 }
86161}
0 commit comments