1

Closed

Move configuration initialization into HttpConfiguration

description

Today:
HttpServer has a protected virtual Initialize called just before it starts using the config. It uses this to setup tracing and formatter loggers.

Problem:
  • HttpServer should not have private knowledge of magic services to initialize
  • User must override HttpServer just to alter behavior of the config -- awkward for WebHost.
Proposal:
  • Add protected virtual Initialize() to HttpConfiguration
  • Move HttpServer config initialization to this new method and call from HttpServer
Benefits:
  • Config just-in-time initialization lives in one place -- in the config itself
  • Users (e.g. Glimpse) can override this virtual for custom just-in-time init related to config.
  • We provide a future hook for ourselves to install immutable guards so users cannot modify the config after it has locked down.
Closed Jul 23, 2012 at 10:51 PM by kichalla
Verified.

comments

roncain wrote May 14, 2012 at 12:57 PM

This was fixed via commit: http://aspnetwebstack.codeplex.com/SourceControl/changeset/changes/2cdc79648632

Changes were:

1) HttpConfiguration now has a public property called Initializer that gets or sets an Action<HttpConfiguration>.

2) This action is responsible for performing last minute just-in-time initialization of the config before it becomes immutable. By default, this executes the default code for WebApi, which includes installing the tracing layer and initializing a property in the MediaTypeFormatters

3) This code originally lived in HttpServer but has been replaced with a call to this new initializer (config.Initializer(config)). It is called immediately before the server processes the first request to perform just-in-time initialization.

4) Just-in-time initialization is intended for scenarios where the configuration needs to be modified at the very last minute before it becomes immutable. It is where custom code can examine the configuration in its entirety and make final config-wide changes. Examples include inserting new services that wrap existing services, verifying configuration correctness, altering MediaTypeFormatters, etc.

5) Users wishing to perform custom just-in-time initialization can set their own Action into the HttpConfiguration.Initializer property prior to processing the first request.

6) To augment existing default initialization logic, set it to an Action that wraps the Action already there. To completely replace the default initialization logic, set it to a custom action that does not invoke that Action that was already there.

This feature works in conjunction with per-controller configurations, commited via http://aspnetwebstack.codeplex.com/SourceControl/changeset/changes/fcfc9f49f96a

When a controller contributes its own per-controller configuration, it will be initialized using the same Initializer as the application-wide configuration. This means custom services or formatters added to a single controller's configuration will be initialized the same way as the application configuration. By default, if tracing has been enabled in the application configuration, per-controller custom services or formatters will also trace.