Rest Sharp Execute<Int> fails due to cast exception

拈花ヽ惹草 提交于 2019-12-08 08:45:20

问题


I have this:

public Int32 NumberOfLocationsForCompany(int companyId)
{
        var response = _curl.ResetRequest()
            .WithPath(LOCATION_URL)
            .AddParam("companyId", companyId.ToString())
            .RequestAsGet()
            .ProcessRequest<Int32>();

        return response;
}

that calls this at the end.

    public T ProcessRequest<T>() where T : new()
    {
        var response = _client.Execute<T>(_request);

        if (response.ErrorException != null)
        {
            throw response.ErrorException;
        }
        return response.Data;
    }

but I get this error. I don't get why it's trying to map an int to a collection or why it's Int64 vs the 32 I specified.: Unable to cast object of type 'System.Int64' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]'.

When I hit the api directly this is what I get back

<int xmlns="http://schemas.microsoft.com/2003/10/Serialization/">17</int>

I feel it's something I'm not understanding about Rest Sharp. I tell the execute method to expect an Int, it receives and int, but is trying to map it to a collection. Why and where does the collection come from?

I have noticed that when I look into the base response object's Content the appropriate result "17" is present, why can't Rest Sharp find it? and still where is it finding the Collection?


回答1:


When looking at the response object I found the return value was in Content vs in Data. I found this to be true whenever I was not returning an object or list of objects.

So now when I'm expecting an int, string, bool, etc I use the following and cast the type of the return value:

    public string ProcessRequestWithValue()
    {
        var response = _client.Execute(_request);

        if (response.ErrorException != null)
        {
            throw response.ErrorException;
        }

        return response.Content;
    }

Hope this helps!



来源:https://stackoverflow.com/questions/12678834/rest-sharp-executeint-fails-due-to-cast-exception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!