Skip to main content
Solved

Unable to cast object of type 'System.Threading.Tasks.Task`1[System.Collections.IEnumerable]' to type 'System.Collections.IEnumerable'

  • May 19, 2023
  • 6 replies
  • 3364 views

Forum|alt.badge.img

Hi, I need to call api to retrieve some transactions details from 3rd party application into acumatica erp. For testing, I used below code in my own customization project. This is a POST request and url is also defined there. 

public virtual async Task<IEnumerable> InsertSelectedLines(PXAdapter adapter)
{
foreach (PXResult<SOLine, SOOrder> result in POrdersView.Select())
{

if (lineExt.Selected1 == true)

{
APProformaItemList toBeInserted = new APProformaItemList();

toBeInserted.Ponbr = order.OrderNbr;
toBeInserted.LineNbr = lineNbr++;

var url = String.Format("https://sandbox.corporate-
api.hsbc.com/mock/v2/transactions");
HttpClient client= new HttpClient(); //added reference

client.DefaultRequestHeaders.Add("ContentType","
application/json");

client.DefaultRequestHeaders.Add("x-report-type", "JSON");

string data = "test=something";
//var stringContent = new StringContent(json, Encoding.UTF8,
"application/json");
StringContent queryString = new StringContent(data);
HttpResponseMessage response = await client.PostAsync(url,
queryString);
response.EnsureSuccessStatusCode();
string content = await response.Content.ReadAsStringAsync();
if (response.StatusCode == HttpStatusCode.OK)
{

toBeInserted.Description = "test if";
}
else
{
//throw new Exception(content);
toBeInserted.Description = " else";
}



}
}

}
return adapter.Get();

Above code defined inside a method which is used to add values from smart panel to custom screen details level. I just need to test whether this is working or not, that’s why I’m assigning some testing values to description field. But the time I adding values from the screen, I’m getting the error-

“Unable to cast object of type 'System.Threading.Tasks.Task`1[System.Collections.IEnumerable]' to type 'System.Collections.IEnumerable'”

Can someone help me to find a solution to this or provide me the guidance to get this  done?

Thank you.

Best answer by sweta68

Hi @oshadarodrigo64 

 

In the code snippet below, the method signature is being updated.

public virtual async Task<IEnumerable> InsertSelectedLines(PXAdapter adapter)
{
// Rest of your code...

await Task.CompletedTask; // Add this line if there are no other awaitable tasks in the method

return adapter.Get();
}

By using Task.CompletedTask, you can ensure that the method has an await able task, which allows the correct return type.
 

Hope it helps!

Regards,

Sweta

6 replies

anahizentella94
Jr Varsity III
Forum|alt.badge.img

Hello, I believe the async in the method is causing this error. 


anahizentella94
Jr Varsity III
Forum|alt.badge.img

Can you write the code that is calling the method?


Forum|alt.badge.img

hi @anahizentella94 

thank you for the response. here is the code of calling the method, this method calls for the smart panel which I use to add data.

public virtual IEnumerable myAction(PXAdapter adapter)
{

try
{
if (POrdersView.AskExt() == WebDialogResult.OK)
{
//return InsertSelectedLines(adapter);
return (IEnumerable)InsertSelectedLines(adapter);
}
}

return adapter.Get();

 


darylbowman
Captain II
Forum|alt.badge.img+15

Do you need to use an async Task?

 

Because if not, simply having it return an IEnumerable instead of Task<IEnumerable> should work.


Forum|alt.badge.img

@darylbowman 

yes I need to perform an async task, that’s why I have used async key in the definition. thank you.


Forum|alt.badge.img+9
  • Semi-Pro III
  • Answer
  • May 22, 2023

Hi @oshadarodrigo64 

 

In the code snippet below, the method signature is being updated.

public virtual async Task<IEnumerable> InsertSelectedLines(PXAdapter adapter)
{
// Rest of your code...

await Task.CompletedTask; // Add this line if there are no other awaitable tasks in the method

return adapter.Get();
}

By using Task.CompletedTask, you can ensure that the method has an await able task, which allows the correct return type.
 

Hope it helps!

Regards,

Sweta