Error “{”stateMachine“:{”<>1__state“:-2,”<>t__builder“:{” when run project netcore 


Before you get into this article, if you like to view .Net Core.

Introduction

You may probably worked in async Task and serialization in your .net core project.

The unhandled error thrown is solvable.  

To explain a bit, I used .Net core 2.0 to create a sample demo web api that deals with accessing server to grab authorization code using controller action method. And the two Action Methods one result passed to another method, this is where error while executing.

How error displayed? 

If you notice error, obviously this never been easier to read "{stateMachine: <> 1 __state <> t__builder". 

The System.Runtime.CompilerServices.AsyncTaskMethodBuilder  is the one trying to compile action method used in controller. 

Just ignore below box, it might scare no head and tail  - it is captured from browser while testing endpoint in browser

[System.Collections.Generic.List 1[XXXX.Models.YYY],System.Runtime.CompilerServices.IAsyncStateMachine]'. Path 'stateMachine.<>t__builder'.

What else could be done earlier?

I asked the same question myself as soon as encounter error, - What should I have done before?

Yes - learning Asynchronous Programming, Task<TResult> would help, if we are familiar then mostly may not encounter similar error in the code.

How I encountered in code?

Again, in project web api controller I have two actions methods, as below


Action Method #1  ( HTTP GET )

public IActionResult Callbackurl(string code, string userid)

Action Method #2   ( HTTP GET )

public async Task<IActionResult> Callbackurl(string userid)


What I'm trying to do here is, I supposed to call Action Method #2 (async)  from Action Method #1 (non-async). 

So basically, when calling an async task from a non-async call in a Controller, an un-handled exception thrown here.


Can you Reproduce same Error?

Yes - asking the right question always gets us solutions.

Let's see in detailed non-async and async actions methods with which line of code causes error, In Action method #1 returns Ok( ) calls async method, will show an error at runtime. 

non async method calls async


What is the Solution then?

In asynchronous programming need to ensure return type and action method call uses right keyword.

May be you get weird solution here. 

As simple as a single operator solved which is "await"  

Hey, what to do with keyword? where do I put it? 

This is too simple as you may not have expected that way. 

Tip to remember, when calling from non-async action method to an async action method, make sure you include await before method. And compiler does not allow non-async to async the calling method modifier has to be async opertor which returns Task<IActionResult>

Can you show how to Solve? 

As you can compare the above image and below image different marked in circle.

The below image action method modifier has changed to asnyc Task<IActionResult> along with await operator. 

Please remember this -  When you call a async method in non-async method make sure method modifier changes to  async Task<IActionResult> if not below warning message will be shown in compile time.

Warning

  • The 'await' operator can only be used within an async method. 
  • Consider marking this method with 'async' modifier and changing its return type to 'Task<IActionResult>

 This is the complete solution that solves the problem.


async action method

Summary

So to conclude, this article explains detailed about  non-async and async action method call in a controller with an operator await, how method return type sometimes causes time and energy to find solution and shared a link for asynchronous programming learning minimize the workload. 

This article is written from my experience of resolving a problem.  

To simplify just compare the image 1 and image 2 you will get the solution.