Michael Sync

Michael Sync

Crisp, minimal personal blog

25 Sep 2013

Unirest-RT – Simplified, lightweight HTTP library for Windows 8 RT


Porting Unirest to Windows 8 RT

One of my closed friends from Mashape asked me to help in porting their unirest-net library to Windows 8 RT. So, I took a few hours to port their library for Windows 8 RT runtime. You can take a look at my ported version of Unirest in this github repository.

Here is the list of what I changed in order to make .NET library work in Windows 8 RT runtime. I’m sharing it here because I think that it would be helpful for those who like to port some .NET library to Windows RT.

  • Changed “unirest-net” to “Class Library (Windows Store Apps)”
  • Changed “unirest-net-” to “Unit Test Library (Windows Store Apps)”
  • Added “Microsoft.Net.Http” nuget package that was released with Portable Library for RT and Phone.
  • Added “Newtonsoft.Json” nuget package because System.Web.Script.Serialization is not available in Windows RT

The following changes have been made in “unirest-net” project.

  • File Name: unirest-net/unirest-net/src/http/HttpResponse.cs (Note: — means “remove” and ++ means “added”)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    –using System.Web.Script.Serialization; –else if (typeof(Stream).IsAssignableFrom(typeof(T)))
    ++else if (typeof(Stream).GetTypeInfo().IsAssignableFrom(typeof(T).GetTypeInfo()))
    
    –var serializer = new JavaScriptSerializer(); var stringTask = response.Content.ReadAsStringAsync(); Task.WaitAll(stringTask);
    
    –Body = serializer.Deserialize(stringTask.Result);
    
    ++Body = JsonConvert.DeserializeObject(stringTask.Result); 
     

  • File Name: unirest-net/unirest-net/src/request/HttpRequest.cs

    1
    2
    3
    4
    5
    6
    7
    8
    
    –using System.Web.Script.Serialization;
    
    –var serializer = new JavaScriptSerializer();
    
    –Body = new MultipartContent { new StringContent(serializer.Serialize(body)) };
    
    ++Body = new MultipartContent { new StringContent(JsonConvert.SerializeObject(body)) };
     

The following changes have been maded in “unirest-net-test” project.

  • The original test project was using nunit unit test framework because Nunit Adapter VS extension (beta 5) doesn’t work with Win RT for some reasons. So, I changed it to VS unit test framework and removed the Nunit nuget package.
  • File Name: unirest-net/unirest-net-tests/src/http/UnirestTests.cs. The original code is using the capital “P” (e.g. Unitest.Post(..)..) so I changed to the small letter.

Unirest.post("http://localhost").HttpMethod.Should().Be(HttpMethod.Post);

Unirest.post("http://localhost").URL.OriginalString.Should().Be("http://localhost");

That’s all. Hope that you find it useful. If you like to share your experience and chellenge with porting .NET code to Windows RT, please drop a comment or email me via Contacts form. Thanks!

Happy porting!  (Well, I used to write “Happy Silverlighting!” but no more. :| )