I have an async task for getting messages via entity framework, but I also need to compute the username of senders and recipients. To do this I have a second async task that gets the IdentityUser object for the user by userid and then gets the username for that id.
For instance:
public async Task<Messages[]> GetMessages()
{
return await (from message in _context.Messages
select message into msg
select new Messages
{
Messageid = msg.Messageid,
Subject = msg.Subject,
Recipient = msg.Recipient,
Sender = msg.Sender,
Date = msg.Date,
Msgsender = MsgUserName(msg.Sender).Result,
Msgrecipient = MsgUserName(msg.Recipient).Result
}).ToArrayAsync();
}
That task works for getting messages, but then I need to figure out usernames based on user ids.
public async Task<string> MsgUserName(string userid)
{
IdentityUser user = await _userManager.FindByIdAsync(userid);
return await _userManager.GetUserNameAsync(user);
}
This results in the following error message at runtime: InvalidOperationException: The client projection contains a reference to a constant expression of 'PostAlmostAnything.SiteServices.MessageService' through the instance method 'MsgUserName'. This could potentially cause a memory leak; consider making the method static so that it does not capture constant in the instance. See https://go.microsoft.com/fwlink/?linkid=2103067 for more information.
I tried making it static per the recommendation, but that results in a red squiggly line under _userManager and the error message "An object reference is required for the nonstatic field, method, or property 'MessageService._userManager'"
I figure there are probably two ways around this. Find a way to make the user manager accessible via a static method or find a way to make .Net ignore the potential memory leak and run anyway.
I don't see how this would result in a memory leak. I'm just getting usernames by id for every message.