ASP.NET Identity CultureAwaiter Code

While developing an ASP.NET MVC application, I came across an CultureAwaiter

instance of which is returned when an extension method is called WithCurrentCulture

.

I am relatively fresh to Microsoft's asynchronous model, so I am trying to understand the intuition behind the four lines of the code noted below. Note that I took this from the build file version "2.1.30612.0" using ILSpy ... I don't think MS provided us with an available source.

In these four lines, which I am assuming to run synchronously on the same thread, it looks like the variable is currentCulture

set to the current thread culture (so far so good). Two lines later, however, it just takes that variable and sets the current thread culture for it (i.e. just unassigns). What is the use of this?

UI culture, on the other hand, has slightly different behavior along these four lines. Note the case of "UI" / "Ui" in variable names. On the second of these four lines, a variable is currentUICulture

set to the current culture of the thread's UI (presumably to "remember" it for later use). Two lines later, the current thread UI culture is set to a different variable currentUICulture

(note the other case) ... defined at the beginning of the method.

My newbie understanding the asynchronous model aside, I would at least expect both CurrentCulture and CurrentUICulture to have the same get / setting behavior in this method. I could be completely wrong, but my gut feeling tells me that there might be a misappointment happening in these four lines.

Can anyone shed some light on this for my understanding? Perhaps it has something to do with ILSpy?

// Microsoft.AspNet.Identity.TaskExtensions.CultureAwaiter<T>
public void UnsafeOnCompleted(Action continuation)
{
    CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
    CultureInfo currentUiCulture = Thread.CurrentThread.CurrentUICulture;
    this._task.ConfigureAwait(false).GetAwaiter().UnsafeOnCompleted(delegate
    {
        // WHAT GOING ON IN THE NEXT FOUR LINES?
        CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
        CultureInfo currentUICulture = Thread.CurrentThread.CurrentUICulture;
        Thread.CurrentThread.CurrentCulture = currentCulture;
        Thread.CurrentThread.CurrentUICulture = currentUiCulture;
        try
        {
            continuation();
        }
        finally
        {
            Thread.CurrentThread.CurrentCulture = currentCulture;
            Thread.CurrentThread.CurrentUICulture = currentUICulture;
        }
    });
}

      

+3


source to share


1 answer


The purpose of this is to execute a continuation with the current culture setting, although it might run on a different thread. But we do not want to constantly change the culture of this flow, because we do not have this flow. He shared. Therefore, we must restore the old settings before exiting.

The decompiler is probably just showing misleading variable names. The reflector does it right:



public void UnsafeOnCompleted(Action continuation)
{
    CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
    CultureInfo currentUiCulture = Thread.CurrentThread.CurrentUICulture;
    this._task.ConfigureAwait(false).GetAwaiter().UnsafeOnCompleted(delegate {
        CultureInfo info1 = Thread.CurrentThread.CurrentCulture;
        CultureInfo currentUICulture = Thread.CurrentThread.CurrentUICulture;
        Thread.CurrentThread.CurrentCulture = currentCulture;
        Thread.CurrentThread.CurrentUICulture = currentUiCulture;
        try
        {
            continuation();
        }
        finally
        {
            Thread.CurrentThread.CurrentCulture = info1;
            Thread.CurrentThread.CurrentUICulture = currentUICulture;
        }
    });
}

      

+3


source







All Articles