So I figured it out, I had to first subclass WebServiceHostFactory:
/// <summary>
/// RestServiceFactory extends WebServiceHostFactory and adds support for JSONP encoding
/// </summary>
public class RestServiceFactory : WebServiceHostFactory
{
/// <summary>
/// Creates a ServiceHost using the first address in baseAddresses
/// </summary>
/// <param name="serviceType"></param>
/// <param name="baseAddresses"></param>
/// <returns></returns>
protected override System.ServiceModel.ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
Uri[] defaultAddresses = new Uri[1];
defaultAddresses[0] = baseAddresses[0];
ServiceHost host = new RestServiceHost(serviceType, defaultAddresses);
// Bind up the JSONP extension
CustomBinding cb = new CustomBinding(new WebHttpBinding());
cb.Name = "JSONPBinding";
// Replace the current MessageEncodingBindingElement with the JSONP element
var currentEncoder = cb.Elements.Find<MessageEncodingBindingElement>();
if (currentEncoder != default(MessageEncodingBindingElement))
{
cb.Elements.Remove(currentEncoder);
cb.Elements.Insert(0, new JSONP.JSONPBindingElement());
}
host.AddServiceEndpoint(serviceType.GetInterfaces()[0], cb, defaultAddresses[0]);
return host;
}
}
And then I had to subclass WebServiceHost to modify how it was setting up behaviors:
/// <summary>
/// RestServiceHost extends WebServiceHost to add JSONP support
/// </summary>
public class RestServiceHost : WebServiceHost
{
public RestServiceHost() : base() { }
public RestServiceHost(Type serviceType, params Uri[] baseAddresses) : base(serviceType,baseAddresses) { }
protected override void OnOpening()
{
base.OnOpening();
if (base.Description != null)
{
foreach (ServiceEndpoint endpoint in base.Description.Endpoints)
{
if ((endpoint.Binding != null) && (endpoint.Binding.CreateBindingElements().Find<JSONP.JSONPBindingElement>() != null))
{
if (endpoint.Behaviors.Find<WebHttpBehavior>() == null)
{
endpoint.Behaviors.Add(new WebHttpBehavior());
}
}
}
}
}
}
The reason for this change is because WebServiceHost was not adding any behaviors because of the custom binding.
With these two changes done, the requests were piped through the correct binding extension, no web.config changes needed.