Throwing an exception while deserializer plain bool type from Web Api

1,247 views
Skip to first unread message

bks

unread,
Aug 27, 2012, 9:51:36 AM8/27/12
to rest...@googlegroups.com
My question is basically if RestSharp has problems with primitive types returned by web api?

Here's how I am consuming a ASP.net web api:

        public T Execute<T>(RestRequest request) where T : new()
        { 
            var response = _client.Execute<T>(request);
            if(response.ResponseStatus != ResponseStatus.Completed)
            {
                throw response.ErrorException;
            }
            return response.Data;
        }

The ASP.net Web api has an action:

        [HttpPost]
        public bool Login(UserAccount account)
        {
            return true;
        }

When I call this web service fiddler, I see this:

HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Mon, 27 Aug 2012 13:19:10 GMT
X-AspNet-Version: 4.0.30319
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Content-Type: application/json; charset=utf-8
Content-Length: 4
Connection: Close

true

Incidentally, the JSON tab in the Fiddler Response window, has nothing. Does this mean, this response is not well formed JSON? 

When this action is called, RestSharp throws an exception:

{
    "Message": "An error has occurred.",
    "ExceptionMessage": "Unable to cast object of type 'System.Boolean' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]'.",
    "ExceptionType": "System.InvalidCastException",
    "StackTrace": "   
at ServiceManager.Execute[T](RestRequest request) in ServiceManager.cs:line 92
at ServiceManager.Login(UserAccount userAccount) in ServiceManager.cs:line 78
at Login(UserAccount account) in AccountController.cs:line 30
at lambda_method(Closure , Object , Object[] )
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass13.<GetExecutor>b__c(Object instance, Object[] methodParameters)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.<>c__DisplayClass5.<ExecuteAsync>b__4()
at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)"
}

There's more. It seems like the deserializer is choking:

   at RestSharp.Deserializers.JsonDeserializer.FindRoot(String content)
   at RestSharp.Deserializers.JsonDeserializer.Deserialize[T](IRestResponse response)\r\n   at RestSharp.RestClient.Deserialize[T](IRestRequest request, IRestResponse raw)

Any ideas? 

Many thanks!

Peter Johanson

unread,
Aug 27, 2012, 10:21:37 AM8/27/12
to rest...@googlegroups.com
RestSharp assumes a JSON object or array as a return value. It does not
support top-level, bare "values" in responses.

Whether it makes sense to support such a thing is probably worth
discussing on the list. I think there's marginal value, if it does not
majorly disrupt/complicate the JsonDeserializer code.

-pete
--

bks

unread,
Aug 27, 2012, 1:28:46 PM8/27/12
to rest...@googlegroups.com
Thanks for the response Pete.

Any ideas why Asp.net Web api is not serializing the bool value into  JSON object. I would assume that the return results are going through the Json serializer.

If I replace the primitive bool type and wrap it in an object with a bool value in it, everything works fine!

Peter Johanson

unread,
Aug 27, 2012, 1:31:33 PM8/27/12
to rest...@googlegroups.com
Well, in order to serialize the bool into a JSON object, you'd need to
know/default a field name for that boolean, e.g. { "value": true }.
Using some automatic field name is a bit horky, and possibly unexpected.

-pete
> > Server: [1]ASP.NET Development Server/[2]10.0.0.0
> References
>
> 1. http://ASP.NET/
> 2. http://10.0.0.0/

--

Patrick Steele

unread,
Aug 27, 2012, 1:33:57 PM8/27/12
to rest...@googlegroups.com
I think you want to update the signature of your controller method:

public ActionResult Login(UserAccount account)
{
return Json(true);
}

---
Patrick Steele
http://weblogs.asp.net/psteele

Patrick Steele

unread,
Aug 27, 2012, 1:41:44 PM8/27/12
to rest...@googlegroups.com
Oh yeah -- forgot about that. My answer should have been:

var result = new {value = true};

---
Patrick Steele
http://weblogs.asp.net/psteele


Badi Sudhakaran

unread,
Aug 27, 2012, 2:04:34 PM8/27/12
to rest...@googlegroups.com
Thanks guys!

So, no need for Json() call? Something like this, then:

ActionResult Login(UserAccount account)
{
   var result = new {value=true};
   return result;
}

Sorry, my dev env is not around to test it quickly.

Patrick Steele

unread,
Aug 27, 2012, 2:11:58 PM8/27/12
to rest...@googlegroups.com
No, you still need the Json() call. That will tell MVC to serialize
the object into a json-formatted string and return that string to the
caller.

---
Patrick Steele
http://weblogs.asp.net/psteele


bks

unread,
Aug 27, 2012, 2:38:08 PM8/27/12
to rest...@googlegroups.com
Thanks Patrick. I am using ASP.net Web api and not ASP.Net MVC.

When I did this, it says method, delegate or event expected.

        public ActionResult Login(UserAccount account)
        {
            var result = new {value = true};
            return Json(result);

Patrick Steele

unread,
Aug 27, 2012, 2:44:24 PM8/27/12
to rest...@googlegroups.com
Oops. Sorry -- yeah, if you're inheriting from ApiController, the
Json serialization is implied.

---
Patrick Steele
http://weblogs.asp.net/psteele


Badi Sudhakaran

unread,
Aug 27, 2012, 2:55:26 PM8/27/12
to rest...@googlegroups.com
Ok, I did this:

        public Object Login(UserAccount account)
        {
            var result = new {value = true};
            return result;
        }

On the client side, I want to use the generic version of the Execute call like this:

_client.Execute<T>(request);

Does this mean I need to create a custom object to deserialize into?

Patrick Steele

unread,
Aug 28, 2012, 9:09:47 AM8/28/12
to rest...@googlegroups.com
Yeah, you might need to defined a return type (it'll be better for
unit testing anyway). I don't think I've ever tried to
serialize/deserialize an anonymous object.

---
Patrick Steele
http://weblogs.asp.net/psteele


Reply all
Reply to author
Forward
0 new messages