RestSharp on Win8

1,417 views
Skip to first unread message

Nicolò Carandini

unread,
Mar 7, 2012, 11:17:55 AM3/7/12
to rest...@googlegroups.com
I'd like to use RestSharp on a Metro Win 8 App (XAML / C#)
 
As expected, NuGet complains on the .NET 4.5:
 
---------------------------------------------------------
Attempting to resolve dependency 'Newtonsoft.Json'.
Successfully installed 'Newtonsoft.Json 4.0.8'.
Successfully installed 'RestSharp 102.7'.
Successfully uninstalled 'Newtonsoft.Json 4.0.8'.
 
Install failed. Rolling back...
Could not install package 'Newtonsoft.Json 4.0.8'. You are trying to install this package into a project that targets '.NETCore,Version=v4.5', but the package does not contain any assembly references that are compatible with that framework. For more information, contact the package author.
---------------------------------------------------------
 
I assume I need to get the JSON library code and compile it with .NET 4.5
and the same with RestSharp code.
 
Any hint/help on that?

Nicolò Carandini

unread,
Mar 7, 2012, 11:45:26 AM3/7/12
to rest...@googlegroups.com
Ops, not so easy, I need to use those libs on WinRT, so I'll need to port those libraries there, no way to have .NET on a Metro App :-(

John Sheehan

unread,
Mar 7, 2012, 1:13:22 PM3/7/12
to rest...@googlegroups.com
For the record for everyone, moving RestSharp to WinRT would be a
serious undertaking and not worth the effort.

With HttpClient and dynamic available there's far less need for
RestSharp anyway.

Joel Ivory Johnson

unread,
Apr 17, 2012, 8:10:38 PM4/17/12
to rest...@googlegroups.com
I've got it working on Windows RT, at least the parts that I need to use. It was a pain. 

Is there a set of unit test out there that I could use for testing the changes I made locally?

Joel Ivory Johnson

John Sheehan

unread,
Apr 17, 2012, 8:18:00 PM4/17/12
to rest...@googlegroups.com
Can you summarize what you had to change?

Joel Ivory Johnson

unread,
Apr 19, 2012, 11:05:50 AM4/19/12
to rest...@googlegroups.com
I'm not sure that I can summarize the changes in a meaningful way. There were about 108 places in which I had to make changes. There were lots of updates to #if/#elseif statements using the compiler constant NETFX_CORE.  Reflection in WinRT is a little different. Instead of having methods hanging off of the Type object to get carious pieces of information there is a method GetTypeInfo() that passes back an object that contains most of this information.  So I ended up with the following in the XmlSerializer class (just as an example). 

#if NETFX_CORE
            var props = from p in ((Type)objType).GetRuntimeProperties()
                        let indexAttribute = p.GetAttribute<SerializeAsAttribute>()
                        where p.CanRead && p.CanWrite
                        orderby indexAttribute == null ? int.MaxValue : indexAttribute.Index
                        select p;
#else
var props = from p in objType.GetProperties()
let indexAttribute = p.GetAttribute<SerializeAsAttribute>()
where p.CanRead && p.CanWrite
orderby indexAttribute == null ? int.MaxValue : indexAttribute.Index
select p;
#endif

//More general code here
#if NETFX_CORE
                if (propType.GetTypeInfo().IsPrimitive || propType.GetTypeInfo().IsValueType || propType == typeof(string))
#else
                    if (propType.IsPrimitive || propType.IsValueType || propType == typeof(string)) 
#endif


In general WinRT is most similar to Windows Phone (of all the platforms supported by RestSharp).  So there were several #if !WINDOWS_PHONE that were updated to #if !WINDOWS_PHONE && !NETFX_CORE

That's pretty much it. If it tells you anything here are some other random blocks I changed. 

#if NETFX_CORE
                    if (type.GetTypeInfo().IsGenericType)
                    {
                        var genericType = type.GetTypeInfo().GenericTypeArguments[0];
#else
if (type.IsGenericType)
{
var genericType = type.GetGenericArguments()[0];
#endif


//RestClient.cs
#if NETFX_CORE
            var asmName = this.GetType().AssemblyQualifiedName;
            var versionExpression = new System.Text.RegularExpressions.Regex("Version=(?<version>[0-9.]*)");
            var m = versionExpression.Match(asmName);
            string version = String.Empty;
            if (m.Success)
            {
                version = m.Groups["version"].Value;
            }
           
#else
            var assembly = Assembly.GetExecutingAssembly();
           AssemblyName assemblyName = new AssemblyName(assembly.FullName);
var version = assemblyName.Version;
#endif



//Http.Async.cs
#if !SILVERLIGHT && !NETFX_CORE
webRequest.AllowAutoRedirect = FollowRedirects;
#endif


//StringExtensions.cs
public static string UrlDecode(this string input)
{
#if NETFX_CORE
            return Uri.EscapeUriString(input);
#else
return HttpUtility.UrlDecode(input);
#endif
}

Philipp Panfilov

unread,
Apr 19, 2012, 12:29:14 PM4/19/12
to rest...@googlegroups.com
I hope very much to see WinRT support in next release! :)

среда, 7 марта 2012 г., 20:17:55 UTC+4 пользователь Nick60 написал:

Andrew Young

unread,
Apr 19, 2012, 12:56:06 PM4/19/12
to rest...@googlegroups.com
Joel, that looks like a pain to maintain all those #if's. Unless there's a good way to isolate/abstract all those changes there's a slim chance of that getting supported.

Do you have a fork on github with those changes that we can review?

Joel Ivory Johnson

unread,
Apr 19, 2012, 1:27:47 PM4/19/12
to rest...@googlegroups.com
Some of the #if/elses were already there. I just had to add an additional symbol.   The number of #if/else can be reduced through the creation of some utility functions and calling those functions instead of having the conditionally compiled code. The conditional compilation would then be better isolated to those utility methods. so I'll take a look at how much I can reduce this weekend. 

Will open a github account this weekend to make the changes available for review. 

There is one area I did not address. There were some encryption methods being used and I just have those throwing a NotImplementedException. I've not yet looked at how Encryption works in WinRT (and it wasn't needed for what I was working with).  

John Sheehan

unread,
Apr 19, 2012, 3:03:29 PM4/19/12
to rest...@googlegroups.com
Paul Betts suggested you could cut down on some of the #if noise with
extension methods that re-implement the missing methods. I may take a
look at this again soon since there seems to be an uptick in interest.

Joel Ivory Johnson

unread,
Apr 22, 2012, 7:54:42 AM4/22/12
to rest...@googlegroups.com
If you want to see the changes I had made I checked them in.  My fork is at g...@github.com:j2inet/RestSharp.git . Let me know if there is something else I need to do to share it. This is my first time using github.
 
I took a second pass at the conversion. I had used VS Express before so I didn't see the unit test. This time I used VS 11 Beta and it will load all the projects except the Windows Phone project.  I wanted to make sure I didn't break anything before I ran tests over the Metro code.  OAuthTools.GetSignatureMethod() will currently throw a NoImplementedException but I wanted to test the rest.  But I can't seem to run any of the unit tests. I get the following output when I try.
 

------ Discover test started ------

Unable to load types from the test source 'C:\shares\projects\OpenSource\RestSharp\RestSharp.Silverlight\Bin\Debug\RestSharp.Silverlight.dll'. Some or all of the tests in this source may not be discovered. If you are running unit tests for Windows Metro style app and referencing custom WinMD type then please visit http://go.microsoft.com/fwlink/?LinkId=238340 for more info. Error details: System.IO.FileNotFoundException: Could not load file or assembly 'System.Net, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' or one of its dependencies. The system cannot find the file specified.

.

========== Discover test finished: 0 found (00:00:00.9110351) ==========
------ Run test started ------
The underlying provider failed on Open.
========== Run test finished: 0 run (00:00:00.0359971) ==========

 
 
 

Jason Moore

unread,
Apr 24, 2012, 7:21:48 PM4/24/12
to rest...@googlegroups.com
I tried to port it a few days ago too.  Did some very similar things to Joel (taking John's suggestion to use extensions methods).


I didn't port all the features, only the ones I needed.  I'll have a look at Joel's fork to see how he supported the IDictionary type.

Philipp Panfilov

unread,
Apr 30, 2012, 12:32:21 PM4/30/12
to rest...@googlegroups.com
I have implemented Signature generation in your lib! :)

You can use this code:

case OAuthSignatureMethod.HmacSha1:
{
#if NETFX_CORE
                    //throw new NotImplementedException("What goes here?");
                    IBuffer KeyMaterial = CryptographicBuffer.ConvertStringToBinary(consumerSecret + "&" + tokenSecret, BinaryStringEncoding.Utf8);
                    MacAlgorithmProvider HmacSha1Provider = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA1");
                    CryptographicKey MacKey = HmacSha1Provider.CreateKey(KeyMaterial);
                    IBuffer DataToBeSigned = CryptographicBuffer.ConvertStringToBinary(signatureBase, BinaryStringEncoding.Utf8);
                    IBuffer SignatureBuffer = CryptographicEngine.Sign(MacKey, DataToBeSigned);
                    signature = CryptographicBuffer.EncodeToBase64String(SignatureBuffer);
                    break;

#else

воскресенье, 22 апреля 2012 г., 15:54:42 UTC+4 пользователь Joel Ivory Johnson написал:

Philipp Panfilov

unread,
May 1, 2012, 1:58:46 AM5/1/12
to rest...@googlegroups.com
But I have problem with complex parameters. I'm working on twitter app and
if I making something like this:         

timeLine.AddParameter("status", "Good morning");
//timeLine - my RestRequest 
I recieve 401 error (no Authorization)

If I UrlEncode text:
            var txt = UrlEncode("Good Morning");
            timeLine.AddParameter("status", txt);

Everything is ok, but in twitter I see 

Good%20Morning

So, it wasn''t url decoded



понедельник, 30 апреля 2012 г., 20:32:21 UTC+4 пользователь Philipp Panfilov написал:
Reply all
Reply to author
Forward
0 new messages