Skip to content

IConfigurable

Martin Halliday edited this page Jul 25, 2016 · 2 revisions

This interface can optionally be implemented by middleware to indicate that the middleware can be configured by the application developer.

If the application developer tries to provide configuration to middleware that does not implement the IConfigurable interface this is not an error.

If the application developer does not provide configuration to middleware that does implement IConfigurable the middleware is responsible for adopting a sensible default configuration.

When the application developer is defining their OWIN pipeline using the pipeline builder they can optionally call the fluid ConfigureWith() method to define how the middleware should retrieve its configuration options. When the application developer calls the ConfigureWith() method and the middleware implements the IConfigurable interface then the Configure() method of the middleware will be called.

The Configure() method is defined as void Configure(IConfiguration configuration, string path); where:

Parameter Meaning
configuration This is the configuration mechanism chosen by the application developer
path This is the location within the configuration file where this middleware will find its configuration. The path parameter is a string containing / to identify a node in the configuration file similar to the way that / is used in the path part of a URL. This assumes that the configuration file has a hierarchical structure (for example XML or JSON)

The reason for passing a path is so that the same middleware can be included more than once in the OWIN pipeline with different configuration.

Consider the following example:

  • I have some middleware that returns a 404 response (Not Found) that allows configuration of the body of the response and the content-type to return.
  • I have two routes in my OWIN pipeline, one for handling .aspx requests and one for handling REST API requests. The `.aspx' route always returns html and the REST API route always returns JSON.
  • When requests are received for invalid URLs I want to return a 404 response using the 404 middleware.

In this case I would add two instances of the 404 middleware. I would add one instance of the 404 middleware that executes after the .aspx presentation middleware that returns an html body. I would add another instance of the 404 middleware that executes after the REST API middleware that returns a JSON body. These two instances of the 404 middleware are configured differently and therefore need to be in different areas of the configuration file.

Giving the application developer controls over the location of middleware configuration also avoids conflicts between middleware from different authors, and allows the application developer to structure their configuration file in a way that makes sense for their system.

Clone this wiki locally