异步方法在session_start时有问题会议? [英] Session issue when having async Session_Start method?

查看:220
本文介绍了异步方法在session_start时有问题会议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用.NET 4.5比.NET 4.0,而我热爱异步功能。

I recently started using .NET 4.5 over .NET 4.0, and I am loving the async feature.

然而,在我的Global.asax中在session_start事件在ASP .NET中使用它的时候,无论我做什么(如果我执行的await和访问会话),它会导致错误。

However, when using it in ASP .NET in the Session_Start event in my Global.asax, no matter what I do (if I perform an await and access a session), it'll cause an error.

下面的代码。我敢肯定,你可以复制的问题。事情是 - ?我该如何解决这个问题。

Here's the code. I'm sure you can replicate the issue. The thing is - how do I solve it?

protected async void Session_Start(object sender, EventArgs e)
{
    var stuff = await UserAccess.RetrieveSomeStuff();
    Session["Stuff"] = stuff;
}



我收到的错误是如下:

The error I am receiving is as follows.

这是异步模块或处理程序,而异步
操作仍有待完成。

An asynchronous module or handler completed while an asynchronous operation was still pending.

我也试过,没有运气下面的代码。

I also tried the following code with no luck.

protected void Session_Start(object sender, EventArgs e)
{
    var stuffTask = UserAccess.RetrieveSomeStuff();
    stuffTask.Wait();

    Session["Stuff"] = stuffTask.Result;
}

在那种情况下,这将有 WaitingForActivation 不断,这可能表明它没有被放入任务队列由系统(也许是因为系统正在等待等待(执行)调用自身来完成?)。

In that scenario, it'll have a status of WaitingForActivation constantly, which could indicate that it's not being put into the queue of tasks to be executed by the system (maybe because the system is waiting for the Wait() call itself to finish?).

我也试过手动启动该任务。在这种情况下,它只是崩溃,因为你不能手动启动的任务。它必须由系统就我而言做。

I also tried starting the task manually. In that case, it just crashes, as you can't manually start a task. It has to be done by the system as far as I am concerned.

任何线索?这一个是艰难的。

Any clue? This one is tough.

推荐答案

出现InvalidOperationException 你得到的ASP.NET的方式告诉你,你不能有针对该事件的异步的处理程序。

The InvalidOperationException you're getting is ASP.NET's way of telling you that you can't have an async handler for that event.

不过说真的,你想要做的是的启动的某些异步初始化资源,然后(异步)等待稍后当你真正需要它。因此,在你的会话真的应该是一个任务<的对象; T> ,就像这样:

But really, what you want to do is start some asynchronous resource initializing, and then (asynchronously) wait for it later when you actually need it. So the object in your Session should really be a Task<T>, like this:

protected void Session_Start(object sender, EventArgs e)
{
    Session["Stuff"] = LoadMyDataAsync();
}

static async Task<int> LoadMyDataAsync()
{
    await Task.Delay(1000);
    return 13;
}



然后你就可以这样使用它(这个例子是一个ASP.NET MVC 4控制器,但的WebAPI和异步网页可以使用类似的技术):

And then you can use it like this (this example is an ASP.NET MVC 4 controller, but WebAPI and async WebPages can use a similar technique):

public async Task<ActionResult> Index()
{
    ViewBag.Message = await (Session["Stuff"] as Task<int>);
    return View();
}



这是一个有点尴尬,因为会话不是强类型的。

这篇关于异步方法在session_start时有问题会议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆