ASP.NET MVC 3을 사용하고 있습니다. 일단 동기 동작 중 일부를 비동기 동작으로 변경하기로 결정했습니다. 이를 수행하기 전에 샘플 비동기 조치를 작성하여 제대로 작동하는지 테스트합니다. 이것은 다음과 같습니다 :비동기 작업으로 문화권 초기화하기
public void SampleAsync()
{
AsyncManager.OutstandingOperations.Increment();
var task = System.Threading.Tasks.Task.Factory.StartNew(() => DoStuff());
task.ContinueWith(t =>
{
AsyncManager.Parameters["pars"] = t.Result;
AsyncManager.OutstandingOperations.Decrement();
});
}
private object DoStuff()
{
System.Threading.Thread.Sleep(20000);
return null;
}
public ActionResult SampleCompleted(object pars)
{
return View();
}
그러나이 작업은 비동기 적으로 작동하지 않았습니다! 그것은 절대적으로 동기식이었다. 적어도, 다른 요청 (실행 중)을 전통적인 작업으로 차단하고있었습니다. 조사의 몇 시간 후 나는 문제가 나는 현재의 문화를 초기화 한 내 asax 있다는 발견했습니다 :이 방법에서 주석 때
protected void Application_AcquireRequestState(object sender, EventArgs e)
{
CultureInfo ci;
//It's important to check whether session object is ready
if (HttpContext.Current.Session != null)
{
ci = (CultureInfo)this.Session["Culture"];
//Checking first if there is no value in session
//and set default language
//this can happen for first user's request
if (ci == null)
{
ci = GetCultureFromCookie();
this.Session["Culture"] = ci;
}
if (ci == null)
{
ci = GetStandardCulture();
this.Session["Culture"] = ci;
}
}
else
{
ci = GetCultureFromCookie();
if (ci == null) ci = GetStandardCulture();
}
//Finally setting culture for each request
Thread.CurrentThread.CurrentUICulture = ci;
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);
}
는, 모든 것이 잘 작동하기 시작했다. 문제는 동작 완료 스레드에서 HttpContext.Current에 액세스하는 것과 관련 있다고 가정합니다. 그러나이 경우 사용자의 현재 문화를 어떻게 초기화해야합니까?
this.Session["Culture"] = ci;
당신은 세션에 작성 :
아주 훌륭한 종합 답변입니다. 고맙습니다! –