Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, March 14, 2017 6:51 AM
Hi,
I use AutoMapper on my asp.net web api project.
I am not expert on Task based coding. The error : cannot convert from 'System.Threading.Tasks.Task<Entities.User>' to 'Entities.User'
I know that I should add Task<> but I don't know where should I add (inc. profile etc..) ?
//UserController code
public Task<UserResponse> Get(int id)
{
var user = _userEngine.GetAsync(id);
var userResponse = base.Mapper.Map<User, UserResponse>(user);
return userResponse;
}
//Profile code
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<User, UserResponse>();
}
}
All replies (7)
Tuesday, March 14, 2017 8:02 AM ✅Answered | 1 vote
Async methods return Task<T>, so you need to pass the result of the Task which will be object of type User currently you are passing whole Task to Map method , await the task to complete and then pass the **Result **to the Map method:
var userTask = _userEngine.GetAsync(id); // userTask is Task<User> here
var user = await userTask; // Noe extract the User object from Task<User>
var userResponse = base.Mapper.Map<User, UserResponse>(user); // now use it here
return userResponse;
Hope it helps!
[If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]
Blog | LinkedIn | Stack Overflow | Facebook
Tuesday, March 14, 2017 9:19 AM ✅Answered
I missed the bit that you didn't specified the method to be async, you have two options, the better option is to mark the method async, but if you don't want to do that, then you would need to access the Result property like:
public Task<UserResponse> Get(int id)
{
var user = _userEngine.GetAsync(id).Result;
return base.Mapper.Map<User, UserResponse>(user);
}
The above way will be a blocking synchronous blocking for the synchronous method call.
But note that this can lead to deadlock situations, please refer to the following post for more details:
http://stackoverflow.com/a/12484535/1875256
Hope it helps!
[If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]
Blog | LinkedIn | Stack Overflow | Facebook
Tuesday, March 14, 2017 8:40 AM
Thank you Ehsan
After applying your code, it gives an error "the await operator can only be used within an async method" and it suggests me as potential fixes to add "async" keyword on my method. After adding async, it works but as I said before I am newbie on async codes so I don't know it is safe to use my controller method like this. The code is below. What's your opinion ?
public async Task<UserResponse> Get(int id)
{
var user = await _userEngine.GetAsync(id);
return base.Mapper.Map<User, UserResponse>(user);
}
Tuesday, March 14, 2017 9:34 AM
Hi İlker Yüksel,
This is Visual C# forum. As your issue is related to Web, please ask your question into ASP.Net forum for getting quick response.
Your understanding and cooperation will be grateful.
Thanks,
Sabah Shariq
[If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]
Tuesday, March 14, 2017 10:30 AM
I missed the bit that you didn't specified the method to be async, you have two options, the better option is to mark the method async, but if you don't want to do that, then you would need to access the Result property like:
public Task<UserResponse> Get(int id) { var user = _userEngine.GetAsync(id).Result; return base.Mapper.Map<User, UserResponse>(user); }
The above way will be a blocking synchronous blocking for the synchronous method call.
But note that this can lead to deadlock situations, please refer to the following post for more details:
http://stackoverflow.com/a/12484535/1875256
Hope it helps!
[If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click
"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]
Blog | LinkedIn | Stack Overflow | Facebook
Thank you so much
Tuesday, March 14, 2017 10:43 AM
You are welcome! Glad it helped.
[If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]
Blog | LinkedIn | Stack Overflow | Facebook
Tuesday, March 14, 2017 10:44 AM | 1 vote
@Sabah Shariq it is c# related question about async and await keyword.
[If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]
Blog | LinkedIn | Stack Overflow | Facebook