0

I'm getting this error when I call a Web API method that returns a bool (EDIT - any object, the error comes from the Hub attribute):

Error converting value True to type 'System.Web.Http.IHttpActionResult

This is the method:

    [HttpGet]
    [Hub(Name = "ServiceLog", IncomingMethod = "logIncoming", OutgoingMethod = "logOutgoing")]
    [ResponseType(typeof(bool))]            
    public async Task<IHttpActionResult> TestMethodAsync(int i)
    {
        var result = await _repository.TestMethodAsync(i).ConfigureAwait(false);
        return Ok(result);
    }

EDIT: This is the offending code in the Hub attribute:

public override async void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    {
     ...    
        if (hub != null)
        {
            // THIS LINE THROWS THE ERROR
            var response = await actionExecutedContext.Response.Content.ReadAsAsync(actionExecutedContext.ActionContext.ActionDescriptor.ReturnType);
            hub.Clients.All.Invoke(OutgoingMethod, new { Time = DateTime.Now.ToString("G"), Data = response });
         }
    }

From Fiddler and browsers, I get back a 200 response and the correct return value, but Visual Studio throws an error. What is causing this error?

Big Daddy
  • 5,160
  • 5
  • 46
  • 76
  • 2
    I hope this is useful: [Best practice to call ConfigureAwait for all server side code](http://stackoverflow.com/questions/13489065/best-practice-to-call-configureawait-for-all-server-side-code). Quoting [@StephenCleary](http://stackoverflow.com/users/263693/stephen-cleary): `[...] However, with ASP.NET Web Api, if your request is coming in on one thread, and you await some function and call ConfigureAwait(false) that could potentially put you on a different thread when you are returning the final result of your ApiController function.` Maybe that has something to do with your problem? – Matias Cicero Aug 19 '15 at 19:20
  • 1
    Are you sure that _repository.TestMethodAsync() returns Task ? – kmc059000 Aug 19 '15 at 19:25
  • 1
    It might be that the ResponseType is a primitive. It tries to serialize the data but there's no object, just a value. – Crowcoder Aug 19 '15 at 19:48
  • @MatiCicero...I am async all they way – Big Daddy Aug 19 '15 at 22:45
  • @kmc059000...yes, the repo returns Task – Big Daddy Aug 19 '15 at 22:45
  • @Crowcoder...I think you're on to something, but why does it throw? – Big Daddy Aug 19 '15 at 22:47

0 Answers0