在ASP.NET Core中HttpContext异步安全吗? [英] is HttpContext async safe in asp.net core?

查看:50
本文介绍了在ASP.NET Core中HttpContext异步安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于我已阅读的内容 asp.net core 删除了同步上下文.这意味着在 await 调用之后执行代码的线程可能与在 await

Based on what i have read asp.net core have dropped the synchronization context. This means that the thread that executes codes after await call might not be the same one that executes codes before await

HttpContext 仍然可以安全地用于 async 方法中吗?还是可以在 await 调用后获得不同的上下文?

So is HttpContext still safe to use in async methods? or is it possible to get a different context after the await call?

例如在控制器操作中

public async Task<IActionResult> Index()
{
    var context1 = HttpContext;
    await Task.Delay(1000);
    var context2 = HttpContext;
    ....
}

context1是否可以与context2不同?

could context1 be different from context2?

和建议在无控制器方法中获取上下文的方法是通过依赖注入 IHttpContextAccessor

and the recommended way to get the context in none controller method is by dependency injecting IHttpContextAccessor

IHttpContextAccessor.HttpContext async await 模式安全吗?

I.E.context1是否可以与context2不同?

I.E. could context1 be different from context2?

public async void Foo(IHttpContextAccessor accessor)
{
    var context1 = accessor.HttpContext;
    await Task.Delay(1000);
    var context2 = accessor.HttpContext;
}

推荐答案

那么HttpContext在异步方法中仍然可以安全使用吗?还是在await调用之后可以获得不同的上下文?

So is HttpContext still safe to use in async methods? or is it possible to get a different context after the await call?

async HttpContext 以及ASP.NET pre-Core的整个问题是由于这样的事实,即代码通常从 HttpContext 获得 HttpContext.Current .ASP.NET是一个多线程服务器,每个 await 可以在不同的线程上恢复.因此,ASP.NET pre-Core必须具有一个 AspNetSynchronizationContext ,该异步管理可在恢复异步代码之前管理设置 HttpContext.Current .

The whole problem with async and HttpContext and ASP.NET pre-Core was due to the fact that code usually got its HttpContext from HttpContext.Current. ASP.NET is a multithreaded server, and each await could resume on a different thread. So ASP.NET pre-Core had to have an AspNetSynchronizationContext that managed setting HttpContext.Current before the asynchronous code resumed.

现代的ASP.NET Core 没有同步上下文.但这很好,因为它没有 HttpContext.Current .获取 HttpContext 实例的唯一方法是通过本地属性(例如,控制器类上的 HttpContext )或依赖项注入( IHttpContextAccessor ).

The modern ASP.NET Core does not have a synchronization context. But that's fine, because it also doesn't have HttpContext.Current. The only way of getting the HttpContext instance is through a local property (e.g., HttpContext on your controller class) or dependency injection (IHttpContextAccessor).

(请注意:上面的说明有点简化-ASP.NET pre-Core同步上下文确实处理了除 HttpContext.Current 之外的其他事情-但相同的整体解释适用于其所有内容其他责任-即在核心世界中没有必要)

(Pedantic note: the explanation above is a bit simplified - the ASP.NET pre-Core synchronization context did handle other things besides HttpContext.Current - but the same overall exaplanation holds for all of its other responsibilities - i.e., they are not necessary in the Core world)

因此,上下文不可能不同.它们是相同的属性-相同的对象实例.ASP.NET pre-Core的问题是 static 属性值 HttpContext.Current ,该值已在ASP.NET Core中删除.

So, it is not possible for the context to be different. They are the same property - the same object instance. The problem with ASP.NET pre-Core was a static property value HttpContext.Current, which has been removed in ASP.NET Core.

这篇关于在ASP.NET Core中HttpContext异步安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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