简体   繁体   English

在ASP.NET Web表单上运行异步任务

[英]Running Async tasks on asp.net webforms

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. 当我从控制台应用程序运行此程序时,我连接到一个API并使用了一个异步库(Koenzomers),它工作得很好,当我从asp.net Webform页面运行它时,我无法正常工作。

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 这个想法是连接到统一控制器并基于mac地址授权用户

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. 目前,页面加载陷入僵局,不确定UI是否正在等待任务结束,并且它永远不会结束。

而不是使用PageAsyncTask,请使用以下代码,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM