InvalidCastException when defining a type for ExecuteAsync

236 views
Skip to first unread message

Kindler Chase

unread,
Apr 16, 2012, 9:55:37 PM4/16/12
to rest...@googlegroups.com
I've just started using RestSharp and recently posted a question on SO about Newtonsoft.Json not being deployed to a referenced project: http://stackoverflow.com/questions/10166851

Everything worked fine (except for the dependency resolution) prior to upgrading to 103.0.0 today. Now I'm receiveing an InvalidCastException when defining a type for the ExecuteAsync method.

Here is the response received when not using a type with ExecuteAsync:

{"timestamp":1334626127,"iat":71,"iah":0,"act":0,"mode":1,"sp":60,"act_sp":60,"hold":0,"fan":0,"oat":64,"oah":42,"wind":6,"solar":0,"door":0,"relay_w2":0,"relay_y2":0,"in1":1,"in2":1,"in3":1}

Class I'm using as the type:

public class Raw
{
public DateTime timestamp { get; set; } // unix timestamp
public int iat { get; set; }
public int iah { get; set; }
public int act { get; set; }
public int mode { get; set; }
public int sp { get; set; }
public int act_sp { get; set; }
public int hold { get; set; }
public int fan { get; set; }
public int oat { get; set; }
public int oah { get; set; }
public int wind { get; set; }
public int solar { get; set; }
public int door { get; set; }
public int relay_w2 { get; set; }
public int relay_y2 { get; set; }
public int in1 { get; set; }
public int in2 { get; set; }
public int in3 { get; set; }
public readonly DateTime Received;

public Raw()
{
Received = DateTime.Now;
}
}

And code for retrieving the response:

public void RequestData()
{
var request = new RestRequest();
request.Resource = "v2";
request.RequestFormat = DataFormat.Json;
request.Method = Method.GET;
request.AddParameter( "action", "data" );

var client = new RestClient( "http://example.com" );
client.ExecuteAsync<Raw>( request, MyCallBack );
}
private void MyCallBack( IRestResponse<Raw> response, RestRequestAsyncHandle handle )
{
// do work
}

Andrew Young

unread,
Apr 17, 2012, 12:03:23 AM4/17/12
to rest...@googlegroups.com
Do you have any idea what property it's choking on? I'm guessing its the timestamp?

Kindler Chase

unread,
Apr 17, 2012, 12:45:32 AM4/17/12
to rest...@googlegroups.com
Before posting, I thought the same thing and tried removing the timestamp. Same error. I then removed all properties but one int and the same error.

John Sheehan

unread,
Apr 17, 2012, 4:10:01 AM4/17/12
to rest...@googlegroups.com
What happens if you move the callback function inline?

Can you inspect the ErrorException further and post the stack trace?

Jochem Toolenaar

unread,
Apr 17, 2012, 5:32:36 AM4/17/12
to RestSharp
Im getting the same error in my Windows Phone project, all worked fine
prior to 103. When I run without defining a type I get the right data
back.

Stacktrace :

at
RestSharp.RestClient.<>c__DisplayClass2a`1.<ExecuteAsync>b__29(IRestResponse
response, RestRequestAsyncHandle asyncHandle)
at RestSharp.RestClient.ProcessResponse(HttpResponse httpResponse,
RestRequestAsyncHandle asyncHandle, Action`2 callback)
at
RestSharp.RestClient.<>c__DisplayClass23.<ExecuteAsync>b__20(HttpResponse
r)
at
RestSharp.RestClient.<>c__DisplayClass25.<>c__DisplayClass27.<ExecuteAsync>b__22(Object
s)
at
System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo
rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object
parameters, CultureInfo culture, Boolean isBinderDefault, Assembly
caller, Boolean verifyAccess, StackCrawlMark& stackMark)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters,
CultureInfo culture, StackCrawlMark& stackMark)
at System.Reflection.MethodBase.Invoke(Object obj, Object[]
parameters)
at System.Delegate.DynamicInvokeOne(Object[] args)
at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
at System.Delegate.DynamicInvoke(Object[] args)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority
priority)
at System.Windows.Threading.Dispatcher.OnInvoke(Object context)
at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)
at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[]
args)
at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr
pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam&
pResult)

Jochem Toolenaar

unread,
Apr 17, 2012, 6:00:01 AM4/17/12
to RestSharp
Tested it a bit further and deserializing after the call seems to
work.


client.ExecuteAsync(request, (response) => {
//remove items that are not streamable
if (response != null)
{
JsonDeserializer deserializer = new
JsonDeserializer();
var items =
deserializer.Deserialize<List<Track>>(response);
}
}

Kindler Chase

unread,
Apr 17, 2012, 7:29:46 PM4/17/12
to rest...@googlegroups.com
I should have stated that this is a WP7 application. 

If I move the callback to an inline method, the same InvalidCastException occurs (which is also the same as Jochem's):

System.InvalidCastException was unhandled
  Message=InvalidCastException
  StackTrace:
And based on Jochem's replies, I have confirmed that not using a type in the ExecutAsync method works correctly when manually creating the JsonDeserializer

private void MyCallBack( IRestResponse response, RestRequestAsyncHandle handle )
{
  // other error checking code ommitted
  var jd = new JsonDeserializer();
  var raw = jd.Deserialize<ThermostatData.Raw>( response );
}

Looks like this may have been a breaking change for the WP7 projects?

Kindler Chase

unread,
Apr 17, 2012, 7:30:27 PM4/17/12
to rest...@googlegroups.com
Thanks Jochem! - this is the fix for WP7 projects.

Nicolò Carandini

unread,
Apr 19, 2012, 4:28:16 AM4/19/12
to rest...@googlegroups.com
I have the same issue on my WP 7.1 project
 
I'm asking myself if this issue happens everytime a WP7 App try to receive JSON data or this issue happen only in few cases.
 
Or maybe depends on my error defining the outboud class used to receive the deserialized data.
 
My case:
 
JSON:
---------
 
{
    "d": [
        {
            "PartitionKey": "Agosta",
            "RowKey": "a094f8eb-da2e-44e9-9662-bd3e72d6fe61",
            "Timestamp": "2012-03-12T16:40:16.2982729Z",
            "entityid": "e44050e8-75bb-4b2c-8271-83d06a000620",
            "denominazione": "Municipio",
            "latitudine": "41.980841",
            "longitudine": "13.033228",
            "indirizzo": "Piazza San Nicola 9",
            "comune": "Agosta"
        },
        ...other omitted items...
    ]
}
 
The outboud class:
-------------------------
public class WiFiHotSpotOutbound
{
    public HotSpotItem[] D { get; set; }
}
 
public class HotSpotItem
{
    public string PartitionKey { get; set; }
    public string RowKey { get; set; }
    public string Timestamp { get; set; }
    public string EntityId { get; set; }
    public string Denominazione { get; set; }
    public string Latitudine { get; set; }
    public string Longitudine { get; set; }
    public string Indirizzo { get; set; }
    public string Comune { get; set; }
}
 
Note: same error even if I define the property D as a List<HotSpotItem>
 

Bevan Blackie

unread,
Apr 23, 2012, 3:47:14 AM4/23/12
to rest...@googlegroups.com
I got the same issue, the work around that Jochem Toolenaar suggested fixed the first problem. However I'm targeting a .net 4 WCF web service that supports both XML (default) and JSON on the same resource using automaticFormatSelectionEnabled="true". However, even though I set the request to:

request.RequestFormat = DataFormat.Json: 

I get an error because I receive XML instead of JSON. When inspecting the request in fiddler I see the following:

Accept: application/json, application/xml, text/json, text/x-json, text/javascript, text/xml

I think I will be rolling my windows phone app back to 1.2.7 where everything worked...

John Sheehan

unread,
Apr 26, 2012, 1:39:09 AM4/26/12
to rest...@googlegroups.com
I believe I have resolved this. I released a beta package for you to test against to confirm:  https://nuget.org/packages/RestSharp/103.1.0-beta 

Kindler Chase

unread,
Apr 26, 2012, 3:58:12 PM4/26/12
to rest...@googlegroups.com
103.1.0-beta does indeed resolve the InvalidCastException.

Thanks!

Josh Earl

unread,
May 3, 2012, 11:29:29 PM5/3/12
to rest...@googlegroups.com
I ran into this issue today and the beta solves it. Thanks!

Hajdrik Attila

unread,
May 4, 2012, 1:58:11 AM5/4/12
to <restsharp@googlegroups.com>
Hi!

Last night John pushed a fix for this and it solved our problem!

Thanks,
Attila

Sent from my iPhone 4S


Sent from Exchange Server 2010
Reply all
Reply to author
Forward
0 new messages