Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>With lots of trial and error I found a very ugly and totally illogic solution to my problem. But still... Maybe this post can help someone in the future. Note that this "solution" works for me in .NET 4.5. I do not guarantee it'll work for you.</p> <p>The problem comes down to this:</p> <ul> <li>it's impossible (AFAIK) to put a escaped forward slash in a Uri in .NET</li> <li>for communicating with an external service (RabbitMQ) I really need to be able put %2f (i.e. forward slash) in my request Url</li> </ul> <p>The following post put me in the "right" direction: <a href="http://mikehadlow.blogspot.be/2011/08/how-to-stop-systemuri-un-escaping.html" rel="nofollow">How to stop System.Uri un-escaping forward slash characters</a></p> <p>I tried the solution proposed in the post, but... to no avail</p> <p>Then after lots of cursing, googling, reverse engineering and so forth i came up with the following piece of code:</p> <pre><code>/// &lt;summary&gt; /// Client enpoint behavior that enables the use of a escaped forward slash between 2 forward slashes in a url /// &lt;/summary&gt; public class EncodeForwardSlashBehavior:IEndpointBehavior { public void Validate(ServiceEndpoint endpoint) { } public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) { } public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { } public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) { clientRuntime.ClientMessageInspectors.Add(new ForwardSlashUrlInspector()); } } /// &lt;summary&gt; /// Inspector that modifies a an Url replacing /// with /%2f/ /// &lt;/summary&gt; public class ForwardSlashUrlInspector:IClientMessageInspector { public object BeforeSendRequest(ref Message request, IClientChannel channel) { string uriString = request.Headers.To.ToString().Replace("///", "/%2f/"); request.Headers.To = new Uri(uriString); AddAllowAnyOtherHostFlagToHttpUriParser(); return null; } /// &lt;summary&gt; /// This is one of the weirdest hacks I ever had to do, so no guarantees can be given to this working all possible scenarios /// What this does is, it adds the AllowAnyOtherHost flag to the private field m_Flag on the UriParser for the http scheme. /// Replacing /// with /%2f/ in the request.Headers.To uri BEFORE calling this method will make sure %2f remains unescaped in your Uri /// Why does this work, I don't know! /// &lt;/summary&gt; private void AddAllowAnyOtherHostFlagToHttpUriParser() { var getSyntaxMethod = typeof(UriParser).GetMethod("GetSyntax", BindingFlags.Static | BindingFlags.NonPublic); if (getSyntaxMethod == null) { throw new MissingMethodException("UriParser", "GetSyntax"); } var uriParser = getSyntaxMethod.Invoke(null, new object[] { "http" }); var flagsField = uriParser.GetType().BaseType.GetField("m_Flags", BindingFlags.Instance|BindingFlags.NonPublic); if (flagsField == null) { throw new MissingFieldException("UriParser", "m_Flags"); } int oldValue = (int)flagsField.GetValue(uriParser); oldValue += 4096; flagsField.SetValue(uriParser, oldValue); } public void AfterReceiveReply(ref Message reply, object correlationState) { } } </code></pre> <p>So basically I'm creating a custom EndpointBehavior that uses reflection to add an enum flag to a private variable inside the UriParser. This apparently prevents the escaped forward slash in my request.Headers.To uri from being unescaped.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload