在下面的代码片段中,SynchronizationContext
丢失了,因此 CurrentCulture
和 CurrentUICulture
也丢失了。 Log()
来自 this answer .
public async Task<ActionResult> Index()
{
Log("before GetAsync");
await new HttpClient().GetAsync("http://www.example.com/")
.ContinueWith(request =>
{
Log("ContinueWith");
request.Result.EnsureSuccessStatusCode();
}, TaskContinuationOptions.AttachedToParent);
return View();
}
static void Log(string message)
{
var ctx = System.Threading.SynchronizationContext.Current;
System.Diagnostics.Debug.Print("{0}; thread: {1}, context: {2}, culture: {3}, uiculture: {4}",
message,
System.Threading.Thread.CurrentThread.ManagedThreadId,
ctx != null ? ctx.GetType().Name : String.Empty,
System.Threading.Thread.CurrentThread.CurrentCulture.Name,
System.Threading.Thread.CurrentThread.CurrentUICulture.Name);
}
这是输出:
before GetAsync; thread: 56, context: AspNetSynchronizationContext, culture: nl, uiculture: nl
ContinueWith; thread: 46, context: , culture: nl-BE, uiculture: en-US
在 GetAsync
之前,culture 和 UI culture 具有我在 Application_BeginRequest
中设置的值。在 ContinueWith
中,缺少上下文,文化设置为浏览器提供的内容,UI 文化设置为一些默认值。
据我所知,所有与 AspNetSynchronizationContext
相关的事情都应该自动发生。我的代码有什么问题?
最佳答案
为了在请求上下文线程上强制调度延续,您需要指定调度延续时应该使用的 TaskScheduler
。
public async Task<ActionResult> Index()
{
Log("before GetAsync");
await new HttpClient().GetAsync("http://www.example.com/")
.ContinueWith(request =>
{
Log("ContinueWith");
request.Result.EnsureSuccessStatusCode();
},
TaskContinuationOptions.AttachedToParent,
CancellationToken.None,
TaskScheduler.FromCurrentSynchronizationContext());
return View();
}
但是,您使用的是 await
,它会自动将延续编码到当前的 SynchronizationContext
。你应该能够做到这一点:
public async Task<ActionResult> Index()
{
Log("before GetAsync");
HttpResponseMessage request = await new HttpClient().GetAsync("http://www.example.com/");
//everything below here is you 'continuation' on the request context
Log("ContinueWith");
request.EnsureSuccessStatusCode();
return View();
}
关于c# - ContinueWith 丢失 SynchronizationContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23213941/