C# HttpServerUtility。在等待异步操作完成时阻止执行
我有一个asp.net mvc应用程序,它带有一个局部视图来呈现用户配置文件名C# HttpServerUtility。在等待异步操作完成时阻止执行,c#,asp.net,asp.net-mvc,asp.net-mvc-5,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 5,我有一个asp.net mvc应用程序,它带有一个局部视图来呈现用户配置文件名 @{ Html.RenderAction("GetPropertiesForUser", "UserProfile"); } @model Inspinia_MVC5_xx.Models.UserProfile <div class="dropdown profile-element"> <span> <img alt="image" class="img-ci
@{ Html.RenderAction("GetPropertiesForUser", "UserProfile"); }
@model Inspinia_MVC5_xx.Models.UserProfile
<div class="dropdown profile-element">
<span>
<img alt="image" class="img-circle" src="~/Images/profile_small.jpg" />
</span>
<a data-toggle="dropdown" class="dropdown-toggle" href="#">
<span class="clear">
<span class="block m-t-xs">
<strong class="font-bold">@Model.displayName</strong>
</span> <span class="text-muted text-xs block">Art Director <b class="caret"></b></span>
</span>
</a>
<ul class="dropdown-menu animated fadeInRight m-t-xs">
<li><a href="@Url.Action("Profile", "AppViews")">Profile</a></li>
<li><a href="@Url.Action("Contacts", "AppViews")">Contacts</a></li>
<li><a href="@Url.Action("Inbox", "Mailbox")">Mailbox</a></li>
<li class="divider"></li>
<li><a href="@Url.Action("Login", "Pages")">Logout</a></li>
</ul>
</div>
<div class="logo-element">
IN+
</div>
代码:
但是我得到了这个错误
HttpServerUtility。在等待异步
要完成的操作。描述:发生未处理的异常
在执行当前web请求期间。请检查
堆栈跟踪以获取有关错误及其位置的详细信息
源于代码
异常详细信息:System.InvalidOperationException:
HttpServerUtility。在等待异步
要完成的操作
源错误:
第4行:第5行:
第6行:@{
Html.RenderActionGetPropertiesForUser,UserProfile;}第7行:
第8行:
如何解决此问题?是否有ExecuteSingle方法而不是ExecuteSingAsync?我不知道。我不知道ExecuteSingleAsync是什么。您可以链接到文档吗?我有完全相同的问题,调用异步控制器函数的子RenderAction会导致HttpServerUtility。执行阻塞问题。这件事发生在我将我的项目从.NET framework 4.5.2升级到4.6.2 MVC5之后,我在网上搜索,发现有人说子渲染不能是aync函数,两年前它计划在MVC6中修复,现在被.NET core取代。对我来说,我没有更好的解决方案,只是将控制器功能更改为同步
public class UserProfileController : Controller
{
public async Task<ActionResult> GetPropertiesForUser()
{
Uri serviceRoot = new Uri(SettingsHelper.AzureAdGraphApiEndPoint);
var token = await AppToken.GetAppTokenAsync();
ActiveDirectoryClient adClient = new ActiveDirectoryClient(
serviceRoot,
async () => await AppToken.GetAppTokenAsync());
string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
Microsoft.Azure.ActiveDirectory.GraphClient.Application app = (Microsoft.Azure.ActiveDirectory.GraphClient.Application)adClient.Applications.Where(
a => a.AppId == SettingsHelper.ClientId).ExecuteSingleAsync().Result;
if (app == null)
{
throw new ApplicationException("Unable to get a reference to application in Azure AD.");
}
string requestUrl = string.Format("https://graph.windows.net/{0}/users/{1}?api-version=1.5", SettingsHelper.Tenant,ClaimsPrincipal.Current.Identities.First().Name);
HttpClient hc = new HttpClient();
hc.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(
"Bearer", token);
HttpResponseMessage hrm = await hc.GetAsync(new Uri(requestUrl));
if (hrm.IsSuccessStatusCode)
{
UserProfile up = JsonConvert.DeserializeObject<UserProfile>(await hrm.Content.ReadAsStringAsync());
return PartialView(up);
//return View("GetPropertiesForUser", new UserProfile
//{
// DisplayName = up.displayName() //I am still missing here to get the display name only
//});
}
else
{
return View();
}
}
}