The Azure Mobile Services .NET backend has a lot of flexibility in how you can configure and use it. It allows you to plug in additional components, change existing components, and configure how it works. Here we describe the main configuration and extensibility points as well as the common path. Typically you should be able to get by with very little — the goal is to be there when you want it and get out of the way when you need it.
The first step is to define which method is to be called on start-up – we call this the bootstrapper. By default the backend looks for a static class called WebApiConfig (the CLR namespace doesn’t matter) with a public static, parameter-less method called Register. This is by far the common way of bootstrapping and is what the tooling will suggest. If the .NET backend finds a Register class then it is simply called and initialization happens from there. A basic example of the static class looks like this:
This will give you the default configuration all-around. Initialize returns the current HttpConfiguration so if you want to add or modify that then you can easily do it right then and there, for example:
However, if you would like to easily swap between different configurations, then you can use the BootstrapperAttribute to identify a class implementing the IBootstrapper interface, for example by declaring it as follows:
The IBootstrapper interface has a single method called Initialize which will get called, for example:
Now, instead of the static Register class, the MyBootstrapper class will get called upon startup.
The next step is for the bootstrapper to call the service configuration. This happens by calling the ServiceConfig.Initialize class like this:
This class is responsible for getting config from the host, extensions, as well as from your code. As mentioned above, it returns the resulting HttpConfiguration which can then be further modified by your code.
The ServiceConfig.Initialize method takes an IConfigBuilder implementation which is responsible for initializing ASP.NET Web API, the OWIN application pipeline, and the Autofac Dependency Injection engine. The .NET backend comes with a default ConfigBuilder implementation which should be sufficient for most cases. It sets up ASP.NET Web API routes, message handlers, filters, etc. which are used by the backend.
The default ConfigBuilder takes two optional arguments: the first is ConfigOptions which is described below, and the second is a delegate which allows you to add additional services to the Autofac Dependency Injection container, for example
Wherever you have a dependency on IDependencyInterface it can now be provided by Autofac. As described in FilipW’s blog, you can even manipulate the OWIN application pipeline as an Autofac dependency:
In addition, it is possible to provide your own IConfigBuilder where you can either modify or otherwise manipulate the default configuration. Here is an example of an IConfigBuilder implementation that inherits from the default implementation and hooks into one of the virtuals:
The ConfigOptions class is a simple property bag which contains easy setup for a bunch of backend settings. For example, you can set the minimum trace level which will get logged or include/exclude the trace categories you want to see. You can also set the minimum authorization level required to access the Diagnostics controller and other baked-in controllers. Finally you can set the default caching policy for which HTTP caching headers should get included on HTTP responses unless they already have been set by the application.
The last extensibility hook for the .NET backend provides a mechanism for extensions to also participate in the configuration. This allows extensions to hook into the configuration code path, for example, to set up their own per-controller configuration etc. This works very similar to the bootstrapper mechanism in that there is an assembly level attribute indicating the type of the extension configuration:
The MyExtensionConfig must implement the IExtensionConfigProvider interface so that the configuration can call it. For example, the MyExtensionConfig can register types with Autofac as well as manipulating the HttpConfiguration for the service:
Don’t panic — the common case is that you need very little of this – typically you would use the static bootstrapper with some ConfigOptions like this:
This is just to show you how go beyond and truly make the .NET backend yours