System.InvalidCastException: Unable to cast object of type 'AsyncStateMachineBox`1[API.Entities.AppUser,API.Repository.UserRepository+<GetMembers>d__10]' to type 'System.IConvertible'. at System.Convert.ToInt32(Object value)

Asjad Butt 71 Reputation points
2022-09-26T07:29:06.843+00:00

Hi Again so this i a method where i get user id and there are two approaches to it one is that i use the get user by id(GetMembers()) method from the repository and use it
repository method
and its implementation
implementation
and when i call this method in the controller the following error occurs
controller

the error isnt much so i use the convert.toint32 method to convert it into an integer
if i do that apprently the error goes away and when i call the controller in postman i get
System.InvalidCastException: Unable to cast object of type 'AsyncStateMachineBox`1[API.Entities.AppUser,API.Repository.UserRepository+<GetMembers>d__10]' to type 'System.IConvertible'.
at System.Convert.ToInt32(Object value)
viusally:
Postman error

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,590 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,990 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,351 Reputation points Microsoft Vendor
    2022-09-26T10:47:22.27+00:00

    Hi @Asjad Butt ,

    244755-image.png

    From the above screenshot, we can know that the GetMembers method will return a Task<AppUser>, instead of an int value.

    244668-capture.png

    But from the above code and the error message, we can know that the GetUserWithLikes method required an Int type parameter. So, it will meet this issue.

    To solve it, you can modify the code as below:

    var sourceUserId = userRepository.GetMemebers(UserId)?.Result?.SourceUserId. //Assume "SourceUserId" is your property name, you can change it to yours.  
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.