0

当我在 webform 或 MVC 系统中使用此代码时,保持挂起并留在这行代码中:

 AppleRegistrationDescription vb = 
await hubClient.CreateAppleNativeRegistrationAsync(token, new string[] { tag });

但在控制台中工作正常

这是我的代码

static async Task<AppleRegistrationDescription> appleregister(string tag, string token)
        {
            var hubClient = NotificationHubClient.CreateClientFromConnectionString("Endpoint=sb://ipluzservicehub.servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=o9DAUFuT1n9AyHfuc8REkwo0W/65WAw1SSG+fNJ/xqg=", "taylors");
            AppleRegistrationDescription vb = await hubClient.CreateAppleNativeRegistrationAsync(token, new string[] { tag });
            return vb;
        }
        public ActionResult Index()
        {
            try
            {
                Task<AppleRegistrationDescription> t = appleregister("MYTag", "19606e2xxxxxxxxxxxxxxxxxx");
                var list = t.Result;
                var id = list.RegistrationId;
            }
            catch
            {
            }
            return View();
        }

谁能帮帮我

4

2 回答 2

0

请确保您使用的是 ASP.NET 4.5,并且您已将httpRuntime.targetFrameworkweb.config 中的元素设置为4.5.

此外,您不应该Result在 ASP.NET(或 WinForms)中使用;正如我在博客中解释的那样,您很容易导致死锁:

public async Task<ActionResult> Index()
{
  try
  {
    Task<AppleRegistrationDescription> t = appleregister("MYTag", "19606e2xxxxxxxxxxxxxxxxxx");
    var list = await t;
    var id = list.RegistrationId;
  }
  catch
  {
  }
  return View();
}
于 2013-09-13T11:28:22.857 回答
0

你正在经历什么样的失败?有时 ASP.NET 应用程序中的异步上下文可能是导致异步调用挂起的罪魁祸首。尝试使用:

 AppleRegistrationDescription vb = await hubClient.CreateAppleNativeRegistrationAsync(token, new string[] { tag }).configureAwait(false);

让我知道!

于 2013-09-13T20:57:52.410 回答