Configuring your WCF Web Apis

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.

var config = HttpHostConfiguration.Create().
        SetInstanceFactory(new MefInstanceFactory(container)).
        SetMessageHandlerFactory(typeof(LoggingChannel), typeof(UriFormatExtensionMessageChannel)).

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,

public interface IInstanceFactory
    object GetInstance(Type serviceType, InstanceContext instanceContext, HttpRequestMessage request);
    void ReleaseInstance(InstanceContext instanceContext, object service);

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,

public class ContactManagerErrorHandler : HttpErrorHandler
   protected override bool OnHandleError(Exception error)
       return false;
   protected override System.Net.Http.HttpResponseMessage OnProvideResponse(Exception error)
       var exception = (HttpResponseException)error;
       var response = exception.Response;
       response.ReasonPhrase = "[Handled]" + response.ReasonPhrase;
       return response;

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,

RouteTable.Routes.MapServiceRoute<ContactResource>("Contact", config);

1 Comment

  • Hi Pablo,

    Just a little comment: in this new version of WCF Web API, the extensibility point to plug the "service instance creator" was renamed to SetResourceFactory.

    To follow this new name, the interface that we use to create our own factory, also was renamed to IResourceFactory, maintaining the same methods that you posted above.

    Thanks for the good stuff that you publish here.

Comments have been disabled for this content.