@@ -19,7 +19,7 @@ public static class OpenStackNet
1919 {
2020 /// <summary>
2121 /// Global configuration which affects OpenStack.NET's behavior.
22- /// <para>Modify using <see cref="Configure(Action{OpenStackNetConfigurationOptions}) "/>.</para>
22+ /// <para>Customize using the <see cref="Configuring "/> event .</para>
2323 /// </summary>
2424 public static OpenStackNetConfigurationOptions Configuration
2525 {
@@ -36,34 +36,19 @@ public static OpenStackNetConfigurationOptions Configuration
3636 private static bool _isConfigured ;
3737
3838 /// <summary>
39- /// <para>DEPRECATED. This no longer needs to be explicityly called, unless you require customizations. In that case, use <see cref="Configure(Action{OpenStackNetConfigurationOptions})"/> .</para>
40- /// Initializes the SDK using the default configuration.
39+ /// Occurs when initializing the global configuration for OpenStack.NET.
4140 /// </summary>
42- [ Obsolete ( "This will be removed in v2.0. Use Configure(Action{OpenStackNetConfigurationOptions}) instead if you need to customize anything." ) ]
43- public static void Configure ( )
44- {
45- Configure ( null ) ;
46- }
41+ public static event Action < OpenStackNetConfigurationOptions > Configuring ;
4742
4843 /// <summary>
49- /// <para>Provides thread-safe accesss to OpenStack.NET's global configuration options.</para>
50- /// <para>Can only be called once at application start-up, before instantiating any OpenStack.NET objects.</para>
51- /// </summary>
52- /// <param name="configure">Additional configuration of OpenStack.NET's global settings.</param>
53- public static void Configure ( Action < OpenStackNetConfigurationOptions > configure )
54- {
55- Configure ( null , configure ) ;
56- }
57-
58- /// <summary>
59- /// <para>DEPRECATED, use <see cref="Configure(Action{OpenStackNetConfigurationOptions})"/> instead.</para>
44+ /// <para>DEPRECATED, use the <see cref="Configuring"/> event instead.</para>
6045 /// <para>Provides thread-safe accesss to OpenStack.NET's global configuration options.</para>
6146 /// <para>Can only be called once at application start-up, before instantiating any OpenStack.NET objects.</para>
6247 /// </summary>
6348 /// <param name="configureFlurl">Addtional configuration of the OpenStack.NET Flurl client settings <seealso cref="Flurl.Http.FlurlHttp.Configure" />.</param>
6449 /// <param name="configure">Additional configuration of OpenStack.NET's global settings.</param>
65- [ Obsolete ( "This will be removed in v2.0. Use Configure(Action{OpenStackNetConfigurationOptions}) instead." ) ]
66- public static void Configure ( Action < FlurlHttpSettings > configureFlurl , Action < OpenStackNetConfigurationOptions > configure )
50+ [ Obsolete ( "This will be removed in v2.0. Use the OpenStackNet.Configuring event instead." ) ]
51+ public static void Configure ( Action < FlurlHttpSettings > configureFlurl = null , Action < OpenStackNetConfigurationOptions > configure = null )
6752 {
6853 lock ( ConfigureLock )
6954 {
@@ -76,25 +61,30 @@ public static void Configure(Action<FlurlHttpSettings> configureFlurl, Action<Op
7661 return ;
7762 }
7863
79- // TODO: Use the line below once we hit 2.0 and configureFlurl is deprecated
80- // _config = new OpenStackNetConfigurationOptions(configure);
81- _config = new OpenStackNetConfigurationOptions ( options =>
82- {
83- configureFlurl ? . Invoke ( options . FlurlHttpSettings ) ;
84- configure ? . Invoke ( options ) ;
85- } ) ;
64+ // Give the application an opportunity to tweak the default config
65+ _config = OpenStackNetConfigurationOptions . Create ( ) ;
66+ Configuring ? . Invoke ( _config ) ;
67+
68+ // Apply legacy custom configuration, removed in 2.0 as it's replaced by the Configuring event
69+ configureFlurl ? . Invoke ( _config . FlurlHttpSettings ) ;
70+
71+ // Finish configuration and lock it
72+ _config . CompleteInitialization ( ) ;
73+
8674 _isConfigured = true ;
8775 }
8876 }
8977
9078 /// <summary>
91- /// Resets all configuration (OpenStack.NET, Flurl and Json.NET) so that <see cref="Configure(Action{OpenStackNetConfigurationOptions})"/> can be called again.
79+ /// <par>Resets all configuration (OpenStack.NET, Flurl and Json.NET).</par>
80+ /// <para>After this is called, you must re-register any <see cref="Configuring"/> event handlers.</para>
9281 /// </summary>
9382 public static void ResetDefaults ( )
9483 {
9584 lock ( ConfigureLock )
9685 {
9786 _config = null ;
87+ Configuring = null ;
9888 _isConfigured = false ;
9989 }
10090 }
@@ -144,30 +134,46 @@ private static string SerializeHttpCall(HttpCall httpCall)
144134
145135 /// <summary>
146136 /// A readonly set of properties that affect OpenStack.NET's behavior.
147- /// <para>To configure, pass a custom action via the static <see cref="OpenStackNet.Configure(Action{OpenStackNetConfigurationOptions}) "/> method .</para>
137+ /// <para>To customize, register an event handler for <see cref="OpenStackNet.Configuring "/>.</para>
148138 /// </summary>
149139 public class OpenStackNetConfigurationOptions
150140 {
151- private readonly bool _isInitialized ;
141+ private bool _isInitialized ;
152142 private readonly FlurlHttpSettings _flurlHttpSettings ;
153143 private readonly JsonSerializerSettings _jsonSerializerSettings ;
154144 private readonly List < ProductInfoHeaderValue > _userAgents ;
155145
156146 /// <summary/>
157- protected internal OpenStackNetConfigurationOptions ( Action < OpenStackNetConfigurationOptions > configure = null )
147+ protected OpenStackNetConfigurationOptions ( )
158148 {
159149 _flurlHttpSettings = new FlurlHttpSettings ( ) ;
160150 _jsonSerializerSettings = new JsonSerializerSettings ( ) ;
161- _userAgents = new List < ProductInfoHeaderValue >
162- {
163- new ProductInfoHeaderValue ( "openstack.net" , GetType ( ) . GetAssemblyFileVersion ( ) )
164- } ;
151+ _userAgents = new List < ProductInfoHeaderValue > ( ) ;
152+ }
153+
154+ /// <summary />
155+ public static event Action < CreateEvent > Creating ;
156+
157+ /// <summary />
158+ internal static OpenStackNetConfigurationOptions Create ( )
159+ {
160+ var createEvent = new CreateEvent ( ) ;
161+ Creating ? . Invoke ( createEvent ) ;
162+ return createEvent . Result ;
163+ }
165164
166- configure ? . Invoke ( this ) ;
165+ /// <summary />
166+ public void CompleteInitialization ( )
167+ {
168+ OnCompleteInitialization ( ) ;
167169 ApplyDefaults ( ) ;
168170 _isInitialized = true ;
169171 }
170172
173+ /// <summary />
174+ protected virtual void OnCompleteInitialization ( )
175+ { }
176+
171177 /// <summary>
172178 /// Custom Flurl.Http configuration settings which are specific to requests made by this SDK.
173179 /// </summary>
@@ -215,6 +221,8 @@ private void ApplyDefaults()
215221 //
216222 // Apply our default settings on top of user customizations, hopefully without clobbering anything
217223 //
224+ UserAgents . Add ( new ProductInfoHeaderValue ( "openstack.net" , GetType ( ) . GetAssemblyFileVersion ( ) ) ) ;
225+
218226 _jsonSerializerSettings . DefaultValueHandling = DefaultValueHandling . Ignore ;
219227 _jsonSerializerSettings . MissingMemberHandling = MissingMemberHandling . Ignore ;
220228 _jsonSerializerSettings . NullValueHandling = NullValueHandling . Ignore ;
@@ -257,5 +265,12 @@ private void SetUserAgentHeader(HttpCall call)
257265 call . Request . Headers . UserAgent . Add ( userAgent ) ;
258266 }
259267 }
268+
269+ /// <summary />
270+ public class CreateEvent
271+ {
272+ /// <summary />
273+ public OpenStackNetConfigurationOptions Result = new OpenStackNetConfigurationOptions ( ) ;
274+ }
260275 }
261276}
0 commit comments