The bits needed....
First of a message inspector to check for the wcf redirect property and remove it from the Message.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class 307MessageInspector :IDispatchMessageInspector | |
{ | |
private const String RedirectPropertyName = "WebHttpRedirect"; | |
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) | |
{ | |
// This explicitly removes WCF generated redirects caused by added/removed slash from uri templates. | |
if (request.Properties.ContainsKey(RedirectPropertyName)) | |
{ | |
request.Properties.Remove(RedirectPropertyName); | |
} | |
return null; | |
} | |
public void BeforeSendReply(ref Message reply, object correlationState) | |
{ | |
// Do nothing as we dont want to override anything in the pipeline. | |
} | |
} |
Next you need to implement a custom contract behavior to make sure its applied high enough up the pipeline to prevent WCF doing its usual redirect.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class 307ContractBehavior : IContractBehavior | |
{ | |
public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint) | |
{ | |
// Not required | |
} | |
// Adds the 307MessageInspector to the contact to prevent 307 redirects on incorrect template match. | |
public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime) | |
{ | |
dispatchRuntime.MessageInspectors.Add(new 307MessageInspector()); | |
} | |
public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime) | |
{ | |
// Not required | |
} | |
public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) | |
{ | |
// Not required | |
} | |
} |
Finally add it to your service host in the OnOpening override.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected override void OnOpening() | |
{ | |
base.OnOpening(); | |
Log.Debug("Applying prevent 307 redirect behavior"); | |
endpoint.Contract.Behaviors.Add(new 307ContractBehavior()); | |
} |