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

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    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.