0

I am connnecting to an API and using an Async library (Koenzomers) when I run this from a console app, it works absolutely fine, when I run it from a asp.net webform page I can't get it to work.

Code I have

if I try to run the task as I run from the console app

                    Task TestAuth = TestOtherLibrary();
                    TestAuth.Wait();

I get the same result.

protected void Page_Load(object sender, EventArgs e)
{
    Response.Write("Authenticating");
    RegisterAsyncTask(new PageAsyncTask(Auth));
}

public async Task Auth()
{
    using (KoenZomers.UniFi.Api.Api UnifiApp = new KoenZomers.UniFi.Api.Api(new System.Uri("https://myserverurl"), "unifisiteid"))
    {
    await UnifiApp.Authenticate("admin", "mysecurepassword");

    }
}

The idea is to connect to the unifi controller and authorize user based on mac address

Currently, I get the page loading in a deadlock, not sure if the UI is waiting for the task to end, and it never ends.

APearTree
  • 51
  • 1
  • 8
  • Have you [set `httpRuntime.targetFramework`](https://devblogs.microsoft.com/aspnet/all-about-httpruntime-targetframework/)? – Stephen Cleary Jun 28 '19 at 11:44
  • 2
    Duplicate question with https://stackoverflow.com/questions/47341289/asp-net-call-an-async-method-in-page-load ? – B. Lec Jun 28 '19 at 13:22

1 Answers1

0

Instead of using PageAsyncTask, Use the below one,

var localVariable = Task.Run(async () => await Auth())?.Result;

Vetrivel
  • 1
  • 1