0

when i use this code in webform or MVC system keep hangs and stay in this line of code :

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

but in Console works fine

this is my code

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

can any one help me please

Parv Sharma
  • 12,581
  • 4
  • 48
  • 80
Brad Kiani
  • 271
  • 1
  • 9
  • 20

2 Answers2

0

Please ensure that you're using ASP.NET 4.5, and that you have set the httpRuntime.targetFramework element in your web.config to 4.5.

Also, you shouldn't use Result in ASP.NET (or WinForms); you can easily cause a deadlock, as I explain on my blog:

public async Task<ActionResult> Index()
{
  try
  {
    Task<AppleRegistrationDescription> t = appleregister("MYTag", "19606e2xxxxxxxxxxxxxxxxxx");
    var list = await t;
    var id = list.RegistrationId;
  }
  catch
  {
  }
  return View();
}
Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
0

What kind of failure are you experiencing? Sometimes the async context in ASP.NET app could be the culprit, which causes the async call to hang. Try to use:

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

Let me know!

Elio Damaggio
  • 862
  • 4
  • 6