Skip to content

Declarative Binding & Behavior Overview

mikeobrien edited this page Sep 14, 2010 · 2 revisions

The WCF/REST zero configuration approach really simplifies the process of creating and maintaining REST services. Although there may be times where you want to specify bindings and/or behaviors in configuration that can be applied to all or some of your RESTful services. The WcfRestContrib.ServiceModel.Web.WebServiceHost enables this happy medium by allowing you to declaratively specify binding and behavior configuration.

To use the WCF REST Contrib WebServiceHost see the WebServiceHost Overview. Once your service is configured to use this service host you can apply the WcfRestContrib.ServiceModel.Web.ServiceConfigurationAttribute to your service and specify a binding and/or behavior configuration to use.

Binding Configuration

Say for example we have the following binding for streamed http services where we want to specify the max message size in configuration. WCF REST converts bindings into CustomBinding’s so we can declare our configuration as follows:

<bindings>
    <customBinding>
        <binding name="StreamedRest">
            <httpTransport maxReceivedMessageSize="209715200" manualAddressing="true"/>
        </binding>
    </customBinding>
</bindings>

We would apply the ServiceConfigurationAttribute to our service specifying streamed transfer mode and the “StreamedRest” binding:

[ServiceConfiguration(TransferMode.Streamed, "StreamedRest")]
public class Content : IContentService {...}

The WCF REST WebServiceHost will merge the behavior specified in configuration with existing behavior on all endpoints. The only exception to this is if the binding configuration contains a transport binding element. In this case the configuration will only be applied to endpoints whose binding shares the same type of transport binding element. In the example above, the binding configuration will only be merged with bindings on endpoints that also have an http transport binding element. If the above binding configuration contained only non transport binding elements the configuration would be applied to all endpoints. Individual binding elements are replaced by the ones specified in configuration thus overriding existing ones.

Now lets say we have a streamed service that runs over both http and https. In this case we would need to specify 2 binding configurations for each transport as follows:

<bindings>
    <customBinding>
        <binding name="StreamedHttpRest">
            <httpTransport maxReceivedMessageSize="209715200" manualAddressing="true"/>
        </binding>
        <binding name="StreamedHttpsRest">
            <httpsTransport maxReceivedMessageSize="209715200" manualAddressing="true"/>
        </binding>
    </customBinding>
</bindings>

We would apply the ServiceConfigurationAttribute to our service specifying both of our binding configurations:

[ServiceConfiguration(TransferMode.Streamed, "StreamedHttpRest", "StreamedHttpsRest")]
public class Content : IContentService {...}

Behavior Configuration

Specified behavior configuration is applied to all endpoints. Individual behaviors are replaced by the ones specified in configuration thus overriding existing ones. Say for example we have an error handler defined in our behavior:

<behaviors>
    <serviceBehaviors>
        <behavior name="Rest">
            <errorHandler errorHandlerType="..."/>
        </behavior>
    </serviceBehaviors>
</behaviors>

We would apply the ServiceConfigurationAttribute to our service specifying the binding configuration:

[ServiceConfiguration("Rest", true)]
public class Books : IBooksService {...}