Azure Notification Hub不支持webform和MVC,但适用于控制台应用程序

时间:2013-09-13 06:08:59

标签: azure azureservicebus azure-notificationhub

当我在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();
        }

任何人都可以帮助我

2 个答案:

答案 0 :(得分:0)

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

此外,您不应在ASP.NET(或WinForms)中使用Result; you can easily cause a deadlock,正如我在博客上解释的那样:

public async Task<ActionResult> Index()
{
  try
  {
    Task<AppleRegistrationDescription> t = appleregister("MYTag", "19606e2xxxxxxxxxxxxxxxxxx");
    var list = await t;
    var id = list.RegistrationId;
  }
  catch
  {
  }
  return View();
}

答案 1 :(得分:0)

您遇到了什么样的失败? 有时,ASP.NET应用程序中的异步上下文可能是罪魁祸首,这会导致异步调用挂起。 尝试使用:

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

让我知道!