danielwertheim

danielwertheim


notes from a passionate developer

Developer that lives by the mantra "code is meant to be shared".

Share


Tags


Disclaimer

This is a personal blog. The opinions expressed here represent my own and not those of my employer, nor current or previous. All content is published "as is", without warranty of any kind and I don't take any responsibility and can't be liable for any claims, damages or other liabilities that might be caused by the content.

Uri behaves differently in .Net4.0 vs .Net4.5

Daniel WertheimDaniel Wertheim

Got a bug report this week, that MyCouch didn’t support id’s formatted with e.g. a slash person1. So, I was missing an encode of the segment in the Uri. Quite an easy fix. Just identified the few spots where the encode and decode was needed, e.g in my UrlSegment. Added a call to Uri.EscapeDataString running my newly added integration tests. Seeing them become green in .Net4.5 but red in .Net4.0. What!?!?

I’m using the Microsft.Net.Http NuGet, to get the HttpClient in .Net4.0 as well. Quickly isolated the issue to where I create the HttpRequestMessage(HttpMethod method, string requestUri). Passing in an Uri as last argument does not make a difference. I put together a simple test case to see the difference:

Scenario

var u = new Uri("http://localhost:5984/mycouchtests_pri/test%2F1");  
Console.WriteLine(u.OriginalString);  
Console.WriteLine(u.AbsoluteUri);  

Outcome NET4.0

http://localhost:5984/mycouchtests_pri/test%2F1  
http://localhost:5984/mycouchtests_pri/test/1  

Outcome NET4.5

http://localhost:5984/mycouchtests_pri/test%2F1  
http://localhost:5984/mycouchtests_pri/test%2F1  

Do you spot the difference? The one I want is the one in .NET4.5. Please fix this Microsoft. The HttpClient for .Net4.0 should really be on par with the “original” one in .Net4.5. The workaround with adding configuration in e.g. App.config is not applicable. Think of all the SDKs etc that might be based on .Net4.0 HttpClient. You can’t expect the users of those SDK should have to update theirs configuration files.

A NOT APPLICABLE workaround
As described here: http://msdn.microsoft.com/en-us/library/ee656539(v=vs.110).aspx

<configuration>  
  <uri>
    <schemeSettings>
      <add name="http" genericUriParserOptions="DontUnescapePathDotsAndSlashes"/>
    </schemeSettings>
  </uri>
</configuration>  

Earn some StackOverflow points? Provide a “good” workaround here: http://stackoverflow.com/questions/26315934/how-to-workaround-differences-with-uri-and-encoded-urls-in-net4-0-vs-net4-5-us

Thanks,

//Daniel

Developer that lives by the mantra "code is meant to be shared".

Comments