Another major improvement in this new WCF Web Api release is the introduction of a fluent API for configuring your WCF Web Apis. All the available extensibility points in the current bits are now exposed through this API making possible to easily discover them.
Let’s discuss in detail how this new configuration API looks like and what extensibility points you have today.
HttpPostConfiguration is the entry point for the configuration builder and the use you need to use to create a new instance of IHttpHostConfigurationBuilder. As part of this configuration builder, you can find several factories for hooking up the different extensibility extensibility points.
An Operation Handler Factory is responsible for creating new instances of Media Type Formatters and Operation Handlers. A media type formatter knows how to handle specific media types by serializating and deserializing objects into an stream. Typical examples of a Media Formatter are JsonMediaTypeFormatter for “application/Json” or XmlMediaTypeFormatter for “application/xml”. An operation handler on the other hand is what we used to call Http Processor in the previous bits and I already discussed here. In a few words, it is something tied to specific operations that knows how to transform a set of input arguments into some outputs.
An Instance Factory is responsible for creating new service instances. This is the entry point where you can configure your favorite DI framework for creating instances or injecting dependencies on them. An IInstanceFactory (the interface for representing an Instance Factory) looks as follow,
The method names on this interface are self descriptive. The GetInstance method is invoked by WCF to get an instance of an specific service type. The ReleaseInstance is invoked for releasing or disposing that instance when the service has been used already.
A Message Handler Factory is responsible for creating new instances of Http Message Handlers. An Http Message Handler is a new interception mechanism that runs at channel level. I discussed Http Message Channels in detail here.
Finally, an Error Handler is what you can use to map a .NET exception to a Http Response Message. This is where you can set a friendly response message or any specific http status code according to the received exception. The following code shows the error handler sample implementation that comes with the bits,
The resulting IHttpHostConfigurationBuilder instance can later be passed as argument in the constructor to WCF service host (HttpConfigurableServiceHost) or to the Service Route extension method if you are hosting your services in ASP.NET as it is shown bellow,